Wpf: an alternative to the xaml.cs file containing only the deafult constructor and calling InitializeComponent ()

I am using MVVM. Is there a convenient way to not have xaml.cs files, but for some reason still calls InitializeComponent ()?

+3
source share
1 answer

Check this:

<Window x:Class="WpfApplication1.TestBrowser"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Initialize Component Test"
        Height="300"
        Width="300">
    <StackPanel>
        <ListBox x:Name="lb"/>
        <x:Code>
            <![CDATA[
public TestBrowser()
{
  InitializeComponent();
  lb.ItemsSource = new[] { "Hey" };
}
]]>
        </x:Code>
    </StackPanel>
</Window>

When such a XAML file is compiled, the content inside the x: Code element becomes plopped inside the partial class in the .g.cs file.

I still do not recommend this practice. For me, this makes the code less readable, and I'm not sure if Blend or Kaxaml understands it.

+2
source

Source: https://habr.com/ru/post/1735023/


All Articles