Instantiate ViewModels
Posted: 13 Dec 2024, 11:28
I was seeing this sample in your git (https://github.com/Noesis/Tutorials/tre ... ttons/C%23) and I have seen that You instantiate the datacontext in code behind instead instantiate in xaml file and active IntelliCode for properties in ViewModel. Why do you instantiate ViewModel in code behind instead xaml file, it is because instantiate datacontext in xaml file doesnt work with Noesis?
I attach the code (your way and my way). The code of ViewModel is the same both cases
-------------------- YOUR EXAMPLE --------------------
-------------------- MY EXAMPLE --------------------
I attach the code (your way and my way). The code of ViewModel is the same both cases
-------------------- YOUR EXAMPLE --------------------
Code: Select all
namespace TestNoesis
{
/// <summary>
/// Lógica de interacción para TestNoesisWindow.xaml
/// </summary>
public partial class TestNoesisWindow : Window
{
public TestNoesisWindow()
{
System.WeakReference wr = new System.WeakReference(this);
this.Initialized += (s, e) => { ((TestNoesisWindow)wr.Target).OnInitialized(s, e); };
this.InitializeComponent();
}
#if NOESIS
private void InitializeComponent()
{
Noesis.GUI.LoadComponent(this, "/TestNoesis;component/TestNoesisWindow.xaml");
}
#endif
private void OnInitialized(object sender, EventArgs args)
{
this.DataContext = new TestNoesisWindowViewModel();
}
}
}Code: Select all
<Window x:Class="TestNoesis.TestNoesisWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:TestNoesis"
xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
mc:Ignorable="d"
Title="TestNoesisWindow" Height="450" Width="800">
<Window.Resources>
<Storyboard x:Key="Intro">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="StartButton">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1">
<EasingDoubleKeyFrame.EasingFunction>
<BackEase EasingMode="EaseOut" Amplitude="0.5"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>Code: Select all
namespace TestNoesis
{
/// <summary>
/// Lógica de interacción para TestNoesisWindow.xaml
/// </summary>
public partial class TestNoesisWindow : Window
{
public TestNoesisWindow()
{
this.InitializeComponent();
}
#if NOESIS
private void InitializeComponent()
{
Noesis.GUI.LoadComponent(this, "/TestNoesis;component/TestNoesisWindow.xaml");
}
#endif
}
}Code: Select all
<Window x:Class="TestNoesis.TestNoesisWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:TestNoesis"
xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
mc:Ignorable="d"
Title="TestNoesisWindow" Height="450" Width="800">
<Window.DataContext>
<local:TestNoesisWindowViewModel/>
</Window.DataContext>
<Window.Resources>
<Storyboard x:Key="Intro">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="StartButton">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="1">
<EasingDoubleKeyFrame.EasingFunction>
<BackEase EasingMode="EaseOut" Amplitude="0.5"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>