saji8k
Topic Author
Posts: 21
Joined: 07 Jul 2023, 08:35

Loading a Xaml (View) and DataContext (ViewModel) at runtime using C# Managed SDK

22 Nov 2023, 01:56

I was wondering if it is possible to load a specific xaml (View) file with a specific DataContext (ViewModel) class from code using the C# Managed SDK?
If yes, could you provide a quick code example?

I know this is possible using the Unity SDK, you gave a quick code example in this post:
viewtopic.php?t=3115#p16263
However the NoesisView and NoesisXaml classes are specific to the Unity SDK so I am not sure how to do the exact same thing in that post using just the C# Managed SDK.

I don't want to use DataTemplates because that links a specific ViewModel with a specific View. I want to set what XAML file and DataContext classes to use manually (exactly like the Unity SDK).
 
User avatar
sfernandez
Site Admin
Posts: 3011
Joined: 22 Dec 2011, 19:20

Re: Loading a Xaml (View) and DataContext (ViewModel) at runtime using C# Managed SDK

22 Nov 2023, 10:50

Hi,

In C# SDK you can use the Noesis.GUI.LoadXaml() method to load a xaml. The xaml file should be available through the XamlProvider set at initialization (see Integration sample), or if you are using our application framework, then the xaml file should be embedded into the assembly as we do in the samples included in our SDK.
FrameworkElement LoadContent(Panel container, Uri uri, object vm)
{
  FrameworkElement content = Noesis.GUI.LoadXaml(uri) as FrameworkElement; 
  if (content != null)
  {
    content.DataContext = vm;
    container.Children.Add(content);
  }
  return content;
}
And to remove it, you will do as explained in the post you mentioned:
void RemoveContent(Panel container, FrameworkElement content)
{
  container.Children.Remove(content);
}
Hope this helps.

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests