in some Xaml Code i found different usings of e.g. StaticReources.
Sometimes it is x:StaticResources (or x:Static) and sometimes its just StaticResources (or Static).
Can someone explain the difference of the annotations to me please? It seems like both annotations are working, but there should be a difference, shouldn't be?
Related
We recently updated the styling of our app to use WinUI 2.x and while we generally like the changes we have found areas that look terrible and will require overriding the defaults. I looked for a file that would have the default XAML implementation/definitions associated with the new styles, something similar to what's in the generic.xaml file, but I was not able to find it. I would appreciate someone pointing me to this file, if it exists.
~Rich
The source code for the WinUI 2 controls and the XAML templates are available on GitHub.
For example, DropDownButton.xaml.
I have a normal UWP project but I've broken the Application, Views, ViewModels, Models, etc. into their own repositories.
When I reference the View (Page) from the Views repository I get an error |An unhandled exception of type 'System.AccessViolationException'".
If I make the view in the Application level of the project I don't get the error. My repository is a class library and all are pointing to the same Windows 10 version.
Can anyone explain this?
So, a colleague of mine actually found the answer for me. It's more of a hack but it works. I can't explain definitely why this works but I have an assumption. I'll leave the assumption to myself....
To make this work, open your App.xaml file and add the namespace to the Views repository.
xmlns:Views="using:Views"
Then make a dummy resource to the main view in that repository (or any view I'm assuming.)
<Application.Resources>
<Views:MainPage x:Key="mainPage" />
</Application.Resources>
For some reason this makes everything work like a charm... I know it's a hack but it's simple and clean enough that I've bought it and moved on.
For my windows 8 application i am trying to navigate between pages with out using code behind.
For example, i have one image in my UI without creating tapped event for that image i need to navigate to another page,
<Image Source="ms-appx:///Assets/Logo.png" Width="155" Height="110" Tapped="{ // Navigation method here }"/>
Is it possible to navigate between pages like this...? If possible, how can i get this to work??
XAML is just a declarative language without action part so code behind is an essential part of it.
All interactions work via events and event can be handled in a code behind only. So what you want is not possible with XAML(at least with WinRT XAML).
If you are asking if you can specify the code inside the .xaml file, then no, that is not possible.
If you are asking if you can avoid adding code to the .xaml.cs file, then yes, that is possible. You will still need to specify a method but it can even be done as a simple lambda. You will need to use the Command hooks rather than the Event Hooks, e.g.
<Button Command="{Binding GoConnectionCommand}" ... />
The code for this command is usually defined in the ViewModel as part of the MVVM pattern, and Josh Smith explains it far better than I will.
AlSki mentioned using a ViewModel. Although technically the ViewModel is not part of the "code behind" for the XAML file, it's still code and I believe you were asking for a no code solution.
ixSci is correct that there is no way to do this out of the box without code behind in WinRT XAML.
In full WPF it's possible to do this using a behavior called NavigateToScreenAction. You can read about it here. Unfortunately behaviors don't ship out of the box with WinRT, but they can be added back in by an open source project called WinRtBehaviors.
There is no NavigateToScreenAction behavior for WinRT, but one could be created. There is a good article on creating behaviors with the library here. It will obviously require code to create the behavior, but after it's created you could use it in XAML without any code.
Really, the short answer is it's not possible to navigate without code on WinRT.
Dev support, design support and more awesome goodness on the way: http://bit.ly/winappsupport
I am looking to declare an array in XAML. I can do this in WPF. Just can't seem to find the right namespace in WinRT. Anyone know?
<Page xmlns:list="?Something?">
<Page.Resources>
<x:Int32 x:Name="MyScalarValue">123</x:Int32>
<list:Array x:Name="MyValueList">
<x:Int32>123</x:Int32>
<x:Int32>456</x:Int32>
<list:Array>
</Page.Resources>
</Page>
x:Array (and x:Static and a few other ones) aren't presently supported in WinRT. For that matter, x:Array isn't supported in Silverlight either, despite developers pushing for it.
Given the fact that the XAML implementation for WinRT appears to be more closely aligned with SL than WPF, this isn't too surprising.
Edit - some more info regarding SL4+ vs. WPF differences:
"Notable omissions here that exist in WPF or [MS-XAML] are x:Array, x:Code, x:Type, and code access modifiers."
Also, a delta between SL4 and the WinRT implementation here, and its associated links, makes it clear that these bits didn't magically make it into WinRT when they were (and still are) omitted from SL.
I was wondering how I can set a local int variable's value to Int.MaxValue in xaml (which is in the ResourceDictionary in my case).
something like: (but something that works :) )
xmlns:s="clr-namespace:System;assembly=mscorlib
<s:Int32 x:Key"HelloWorld">{x:Static s:Int.MaxValue}</s:Int32>
EDIT:
#Ian:
Thank you :)
But how do I use the static resource as an int?
let's say if I have in my ResourceDictionary
<ResourceDictionary>
<x:Static
x:Key="HelloWorld"
Member="s:Int32.MaxValue"
/>
...
<blablalba TooltipService.ShowDuration="{StaticResource HelloWorld}"/>` <-- this does not work by the way
</ResourceDictionary>
This should do it:
<x:Static
x:Key="HelloWorld"
Member="s:Int32.MaxValue"
/>
Just in case it's not clear what's actually happening here, it's using a handy trick that a lot of people don't know about: although markup extensions like x:Static are normally used in attributes inside braces ({}) you can also use the element syntax for them. This is useful in situations like these where you want to use a markup extension, but the Xaml syntax expects an element. (It's most often necessary in scenarios involving collections, like this one.)
The one slight gotcha with using element syntax for markup extensions is that you no longer get to pass in constructor arguments. (Technically, constructor argument support was introduced in XAML 2009, which has been supported since .NET 4. However, it's only supported if you load Xaml via the new Xaml APIs. The MSDN documentation says that the new XAML 2009 features are not supported for compiled Xaml in WPF.) So you can only use this trick with markup extensions that offer default constructors, and which provide properties that can be used instead of constructor arguments. The StaticExtension provides a Member property that does the trick, so we're OK here, but be aware that sometimes, you'll be stuck due to the inability to call the appropriate constructor.
Note that as you type this in, the Xaml IntelliSense in Visual Studio will probably suggest StaticExtension, which is the real name of this markup extension. When you use the attribute syntax, it leaves off the Extension part, but with elements, it seems not to, for some reason. Either form is correct though - I'd go with what I've written here and not what IntelliSense suggests, because we're more accustomed to seeing "{x:Static ...}" in attributes than "{x:StaticExtension ...}" - again, both are legal, but idiomatically, we tend to leave off the Extension.
You were so close first time, just turns out you missed the 32 from Int32
<blablalba TooltipService.ShowDuration="{x:Static s:Int32.MaxValue}"/>
The other solution of adding a static resource seems a bit ridiculous - you're essentially creating a static resource as a wrapper around an existing static property...
The problem is that there is no class Int If you try with Int32 it works
As such.
<Button>
<ToolTipService.ShowDuration>
<x:Static Member="sys:Int32.MaxValue" />
</ToolTipService.ShowDuration>
</Button>