How to add an Array in WinRT XAML - windows-8

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.

Related

How to bind an event to a command in a Universal App using the MVVM pattern?

I hope somebody can help.
I've spent some time researching the best way to bind an event to a ViewModel command using the MVVM pattern when developing a Universal App. I'm using MVVM Light.
As a test I'm using the SelectionChanged event of a ComboBox.
I've read a few people that have pinched the Behaviours SDK from the Windows 8.1 / WinRT framework and had some success with that. I have also included the Universal App behaviours SDK in my project and tried the following (put together from Windows 8.1 examples but using the UWP SDK).
XAML
<Page
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core" />
...
<ComboBox ItemsSource="{Binding InputQuantities}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction Command="{Binding SomeComboBoxCommand}" CommandParameter="Foo" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</ComboBox>
View Model
public RelayCommand SomeComboBoxCommand {get; set;}
However, the core:InvokeCommandAction isnt part of the Behaviours SDK and i get Invalid Type: expected type is 'Microsoft.Xaml.Interactivity.ActionCollection'. I've tried to use an ActionCollection.... but I'm not sure I know what I'm doing with that.
Ive successfully got it to work with compiled bindings and using Laurent's Blog Post:
XAML
<ComboBox ItemsSource="{Binding InputQuantities}" SelectionChanged="{x:Bind Vm.SomeComboBoxCommand }" />
View Model
public void SomeComboBoxCommand(object sender, SelectionChangedEventArgs e){//do stuff}
I know this isnt what Laurent is intending to demonstrate here and I think doing this is breaking the decoupling of the view and VM by then having to reference a UI component in my view model to get the selected item. But I've seen references to doing this during my research.
So how can I get this working using The Universal App interaction behaviours, if that's the right way to do it of course?
Update 1.
This is what I attempted to add, believing, incorrectly that I was adding the universal app behaviours SDK. I didn't notice at the time that it was targeting Windows 8.1.
However, my questions still stands: Why wont the InvokeActioncommandwork and why is it throwing the mentioned error? I will look at the other posts as soon as I get to work.
Update 2
After testing this on my works PC (exact same code as above, 1st example and the same behaviours SDK) it works fine and I'm getting the behaviour that I would expect. I need to test again on my home PC to see what has gone wrong. (Thanks to Justin XL for sticking with me)
Update 3
For completeness, after returning home I got the latest version of my project (from being checked in on my works PC) and it now also works on my home PC. I'm not sure what state my Visual Studio was in but it had sufficiently confused me enough to post this question. At least this should serve as a document on how to do what is described in the title. Thanks for all your help.
We seem to be getting this question a lot lately, in several different variants...
I'm not familiar with Universal App but is there any specific reason you're trying to use an event? WPF/Silverlight etc are designed to be data driven, all you need to do is bind the ComboBox's SelectedItem member to a property in your view model and the setter will get called whenever the user selects a new item. Often times you have to do exactly the same processing in response to other parts of your view model changing it (e.g. in Master-Child views) so having that logic in a single place generally makes for a much cleaner architecture.
Check this link: MVVM EventBinding Library ,explains about MVVM EventBinding. This purely decouples the View & View model & pass only the arguements to the command.

Metro equivalent for HeaderedItemsControl

Converting a WPF application from .Net 4.0 to Metro.
It uses HeaderedItemsControl in various places.
I have not been able to find that control or a replacement candidate in Metro (Windows.UI.Xaml namespace)
So what is the recommended control in Metro to provide the functionality of HeaderedItemsControl?
You could easily create one by deriving from ItemsControl and adding a few simple dependency properties. You can see which properties are present in the WPF version here. You might not need all of them, but from a quick glance I can see a Header property which is just an object type. You would put a ContentPresenter in your HeaderedItemsControl's ControlTemplate and bind its Content to the HeaderProperty using TemplateBinding. Then bind the HeaderTemplate to the ContentTemplate of the ContentPresenter, etc.
Not sure how useful it is though to port WPF XAML code directly to WinRT. You're just asking for trouble in terms of code compatibility, but also porting a likely desktop-designed UI to a more touch-centric world.

Using RESX with Windows Phone 7 in XAML

I am currently making a basic Windows Phone 7 application for fun, and I am trying to store string (currently only those) values in a RESX file rather than embedding them all within the XAML files directly, or even adding code to manually insert the values.
In WPF, to use the property from the RESX file, you simply map the namespace that contains the RESX:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resx="clr-namespace:Namespace.To.Your.Resx;assembly=SuppliedIfSeparate"
and then use it:
<TextBlock Text="{x:Static resx:Strings.AppTitle}" />
But, I tried, and I've read that x:Static is simply not supported by Silverlight, and by extension, not supported in WP7 (It appears that they have provided the means, but not direct access to x:Static (French) in Silverlight 5/Mango).
I found this post describing an "AppConverter" class that is manually created to leverage similar functionality and it is currently what I am doing. The gist of the post is to create a resource within your XAML page that uses the AppConverter class for pre-specified keys, and then using that to replace the x:Static with a binding.
<TextBlock Text="{Binding Source={StaticResource AppTitle},Converter={StaticResource ResourceRetriever}}" />
It works, but it's verbose and error prone compared to the WPF implementation (a lot of copying and pasting between pages, and even controls).
What is the current best practice for this situation?
MSDN How to: Build a Localized Application for Windows Phone

Is there an IronRuby lib for generating concrete CLR classes?

I want to expose a class to CLR classes. The reason I have is Xaml. I want to write WPF custom controls in Ruby, then use xaml to style and provide templates for them. Last time I tried, Xaml couldn't look up IronRuby types.
class NavBar < TreeView
...
end
<ControlTemlate TargetType={x:Type MyNamspace:NavBar}>
...
</ControlTemplate>
I know I can get there by writing to the CodeDom, but I'm hoping someone already did the heavy lifting or can show me how without resorting to CodeDom.
There is the IronRubyInline project which does exactly that.
For WPF you don't need C# classes though because databinding just works, but for Silverlight < v4 you do need them.
http://github.com/rvernagus/IronRubyInline

is "non-intrusive code-behind" a good or bad practice?

I am a bit surprised that while learning WPF/XAML/Silverlight almost all of the XAML/C# examples I have encountered have the "Click" events in the XAML and very few in the Window or Page constructor.
With all the emphasis these days on "non-intrusive Javascript", I would think that more developers would actually be structuring their XAML/code-behind like this:
XAML:
<Grid>
<Button x:Name="btnEdit"/>
</Grid>
Code behind:
public Window1()
{
InitializeComponent();
btnEdit.Content = "Edit";
btnEdit.Click += new RoutedEventHandler(btnEdit_Click);
}
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
btnEdit.Content = "This button was clicked.";
}
Any thoughts on why this would be a good or bad practice?
Most of the smaller WPF examples give just an impression what is possible without focusing on design issues or good style.
In real world applications XAML should only be used for declarative programming. For example, binding a command to a button or declaring a data binding. Karl Shifflett has some great articles about the MVVM pattern which separates the concerns of your WPF/Silverlight application very well.
Code behind is in my opinion just suitable for tiny applications. It tends to mix view, control and data.
If I remember correctly, I think that there is a partial class which implements the Init code above that is code gened by visual studio. I can’t speak for WPF, but it does this in ASP.Net 2.0, so I’m assuming that it does it the same here. It took me forever to get used to this.
I agree. I hate defining events in the markup.
I agree with your concern.
After much debate, we are following a similar pattern of non-intrusive, ultra-lean XAML and binding commands and data in code-behind.
If you add events in the XAML there is a contextual menu navigation to the event code. If you bind commands in XAML there is no equivalent. You can navigate from the command declaration in the XAML but not where it is assigned to the Command property on a control.
MVVM is bad practice.
You think that you separate Data and View. And what? Total mechanism with XAML binding, binding commands, translating it to methods and implementing INotifyPropertyChanged for what? For UTests (I made - I test conception he-he)? Right software need in only in user's test... It's your code separated in such way in which you sometimes do not understand where is what.
For what you use INotify? For what Microsoft ALL WPF controls and entities as whole in WPF wrote inherited from FrameworkElement with magic DependencyProperties?
Multiple binding is hard resource technique by the word (read authors of MVVM).
I wrote high complicated 3D CAE system without any Patterns less than year...
with classic organization of app with classes and code-behind.
https://skydrive.live.com/?cid=ea6ad1087e3103f0&sc=photos&id=EA6AD1087E3103F0!103&sff=1#cid=EA6AD1087E3103F0&id=EA6AD1087E3103F0!118&sc=photos
All the samples in MVVM are about Customers in Company...
I offer put the name MVVMCC pattern (Customer in Company)