Extract attributes from Xdocument Root which has namespace and prefix - c#

i can't figure out how to get acces to a specific attribute
Xaml starts like :
<CommonControl:PersonnalUserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CommonControl="SomeLibrary"
Width="1920"
Height="1080"
CommonControl:Group="Group1"
CommonControl:Type="Type1"
I just want to get value from "Group", but nothing worked so far :(
I load my Xdocument throught :
xDocument = XDocument.Load(fullPath, LoadOptions.PreserveWhitespace);
Can someone help me please ?

Related

Accessing UWP Child Element Programmatically from Other Class

I have an UWP XAML named MyPage.xaml like below
<Page
x:Class="AffiliaTool.Lib.View.BrowserWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:local="using:AffiliaTool.Lib.View"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tk="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:vm="using:AffiliaTool.Lib.ViewModel"
mc:Ignorable="d">
<tk:TabView x:Name="TabViewBar" />
</Page>
And from other class, I want to instantiate that class and manipulate TabView programmatically like below
var mypage = new MyPage();
mypage.TabViewBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
I used to be able to access Child element programmatically from other class when coding WPF, but why can't I do that in UWP but from codebehind? Anyway to go around this?
Thanks
You can. Just add a FieldModifier like this:
<tk:TabView x:Name="TabViewBar" x:FieldModifier="public"/>
then you can access TabViewBar from another class.
Hope that helps.

How could I access wpf window resoures

I have this xaml
<mui:ModernWindow x:Uid="mui:ModernWindow_1" x:Class="App1.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
ContentSource="/Window1.xaml"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized" MenuLinkGroups="{Binding menuLinkGroups}">
<mui:ModernWindow.Resources>
<sys:String x:Key="ApplicationName">Bla Bla</sys:String>
</mui:ModernWindow.Resources>
<Grid Style="{StaticResource ContentRoot}" Name="mainGrid">
</Grid>
</mui:ModernWindow>
I need to reference current window resources, so I used this:
object obj = this.Resources["ApplicationName"];
But this.Resources doesn't have any resource! so obj is always null. How could I reference this window resources?
Assume that this is a FrameworkElement, like a Window, a Grid, a Button or something like that.
object obj = this.TryFindResource("ApplicationName");
Assuming this is a control...
var parent = Window.GetWindow(this)
Will get the window the control is currently on, you should then be able to access the resources like you already did
parent.Resources["ApplicationName"];
you can use below mentioned code
var MainGrid=Application.Current.FindResource("strApp")
or
this.FindResource("ApplicationName")
Thanks all, I find the solution (I forget to update the localized dll).
I cleaned and rebuilt solution, used locbaml.exe again to generate new localized dll.
You should bind in the XAML and not in the code behind.
"res" is the namespace where the resources file is located.
In your example the namespace alias is "local":
xmlns:local="clr-namespace:Project.Resources"
So your code should look like:
<Page Title ="{x:Static local:localStrings.step1Description}" />
Where:
"local" is the namespace alias where the resources file is located.
"localStrings" is the name of the resource file.
"step1Description" is an entry in the resource file.

TextBox custom control header problems

I'm currently trying to use this TextBox AutoComplete custom control :
http://www.codeproject.com/Articles/26535/WPF-Autocomplete-Textbox-Control?msg=3484969#xx3484969xx
This is the first time I'm adding such external things in my project and when I add the header line in my Xaml file like explained in the article :
<UserControl x:Class="Maha.Gestion.Note_de_frais.SaisieNoteDeFrais"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:WPFAutoCompleteTextbox"
>
VisualStudio tell me that clr-namespace is not included in the assembly.
I'm sure this is something totally trivial but after few hours still can't find any solution.
All files asked by the author of the article has been added of course.
Someone can explain me the good way to add such external resources ?
Thanks
xmlns stands for XML NameSpace. It basically links a prefix of your choice to a namespace. In your case, it links the local prefix with the namespace declared in the file that you want to use.
You can find out more by looking at the XAML Namespaces and Namespace Mapping for WPF XAML article at MSDN.
Please check that the file that you want to use has this exact namespace declared.
Please remove all white space from your xmlns declarations - you may have extraneous characters hidden there.
Like this:
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:WPFAutoCompleteTextbox">

XamlReader and Umlauts

I've got a xaml with a german umlaut (s. below) now when I try to parse it, I get an invalidchar error.
When I don't use an XamlParser Context it works. But I have to use it in order to set some type mappings
XAML:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:test="clr-namespace:BR.UI.Tests.Items;assembly=BR.UI.ViewLocator.Tests"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<test:SampleViewModel />
</UserControl.DataContext>
<Grid>
<Label>รถ</Label>
</Grid>
</UserControl>
Code which parses it
var context = new ParserContext();
var result = System.Windows.Markup.XamlReader.Parse(xaml,context);
I can't find anything to set an encoding hint (which would be .net String UTF-16) what am I doing wrong?
I also tried to inject the encoding with an XmlParserContext.
var xmlcontext = new XmlParserContext(null, null, null, XmlSpace.Preserve,
Encoding.Unicode);
var context = new ParserContext(xmlcontext);
But it didn't help :-(
What do I need to do? Is there some kind of XAML encoding?
Answering my own question hope that is ok.
What helped for me was using the
System.Windows.Markup.XamlReader.Load(stream,XamlParserContext)
Method.
s. http://msdn.microsoft.com/de-de/library/ms590388.aspx
This method seems to respect the encoding of the string variable.
I still don't understand why this doesn't work with the Static ParseMethod. But I hope this solution will help someone else to safe time :-)

Add namespace to XAML code

I'm building an WPF (C#) application and I want to add a namespace to a XAML code (string type). I want to add the namespace on the correct place. Can anyone help me? Thanks, Peter.
EDIT:
It's a XAML code saved in a string like:
<UserControl x:Class="MyTestApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"....
I want to add a new namespace (like xmlns:test="http://www.test.nl") on the correct place.
As you have the XAML in one string, and presumably you have a second string containing the new namespace declaration, it seems that you simply need to use string.Insert to place it in. Your code would be as simple as this:
string xamlString = "... get some xaml from somewhere ...";
int insertPosition = xamlString.IndexOf(">");
xamlString.Insert(insertPosition, "my new namespace");
So I just get the index of the first closing angle bracket, and insert the new namespace right there.

Categories