绑定到位于直接命名空间内的辅助Xaml文件

1tuwyuhd  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(95)

我正在开发的应用程序需要一个WinUI 3 Packaged项目,该项目将生成一个线网格,其中包含由Line标记垂直覆盖的垂直/水平线(由GoogleSheets生成)。我有两个xaml文件,每个垂直/水平xaml代码。我想绑定到MainWindow.xaml这两个文件得到网格上的应用程序的网格面板。我需要一些关于如何获得xaml的想法文件合并到MainWindow.xaml代码中用于演示。我已经尝试搜索一些信息,但还没有找到任何相关的信息。任何想法?(提前感谢)
到目前为止,我已经尝试了以下方法:

MainWindow.xaml:
<Grid>
    Blank -- waiting for some ideas...
</Grid>

V_Grid.xaml:
<Line x:Name="ln1" X1="0" X2="0" Y1="0" Y2="1500" Stroke="#1f1f1f" StrokeThickness=".3" />
<Line x:Name="ln2" X1="10" X2="10" Y1="0" Y2="1500" Stroke="#1f1f1f" StrokeThickness=".3" />
<Line x:Name="ln3" X1="20" X2="20" Y1="0" Y2="1500" Stroke="#1f1f1f" StrokeThickness=".3" />
<Line x:Name="ln4" X1="30" X2="30" Y1="0" Y2="1500" Stroke="#1f1f1f" StrokeThickness=".3" />
<Line x:Name="ln5" X1="40" X2="40" Y1="0" Y2="1500" Stroke="#1f1f1f" StrokeThickness=".3" />
etc...

H_Grid.xaml:
<Line x:Name="ln1" Y1="0" Y2="0" X1="0" X2="2000" Stroke="#1f1f1f" StrokeThickness=".3" />
<Line x:Name="ln2" Y1="10" Y2="10" X1="0" X2="2000" Stroke="#1f1f1f" StrokeThickness=".3" />
<Line x:Name="ln3" Y1="20" Y2="20" X1="0" X2="2000" Stroke="#1f1f1f" StrokeThickness=".3" />
<Line x:Name="ln4" Y1="30" Y2="30" X1="0" X2="2000" Stroke="#1f1f1f" StrokeThickness=".3" />
<Line x:Name="ln5" Y1="40" Y2="40" X1="0" X2="2000" Stroke="#1f1f1f" StrokeThickness=".3" />
etc...

字符串
我试过几个办法都没用。
我尝试了以下方法:

Within Main.xaml:
<Page x:Name="V_Grid" Resources="V_Grid.xaml"></Page>

Within App.xaml:
<ResourceDictionary>
    
    <Page x:Name="V_Grid" x:Key="V_Grid.xaml"></Page>
    
    <ResourceDictionary.MergedDictionaries>
        <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
    </ResourceDictionary.MergedDictionaries>
    
</ResourceDictionary>

(none of these work...)

mspsb9vt

mspsb9vt1#

我想你可能想试试XamlReader
举例来说:

MainPage.xaml

<Grid RowDefinitions="Auto,*">
    <Button
        Grid.Row="0"
        Click="LoadXamlButton_Click"
        Content="Load XAML" />
    <Frame
        x:Name="DynamicFrame"
        Grid.Row="2" />
</Grid>

字符串

MainPage.xaml.cs

private async void LoadXamlButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
    string loadedXamlText = await File.ReadAllTextAsync("V_Grid.xaml");

    if (Microsoft.UI.Xaml.Markup.XamlReader.Load(loadedXamlText) is FrameworkElement dynamicElement)
    {
        this.DynamicFrame.Content = dynamicElement;
    }
}


将被动态加载的文件至少应该具有所需的命名空间。在这种情况下:

V_Grid.xaml

<Grid
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Line
        x:Name="ln1"
        Stroke="Red"
        StrokeThickness=".3"
        X1="0"
        X2="0"
        Y1="0"
        Y2="1500" />
    <Line
        x:Name="ln2"
        Stroke="Green"
        StrokeThickness=".3"
        X1="10"
        X2="10"
        Y1="0"
        Y2="1500" />
    <Line
        x:Name="ln3"
        Stroke="Blue"
        StrokeThickness=".3"
        X1="20"
        X2="20"
        Y1="0"
        Y2="1500" />
    <Line
        x:Name="ln4"
        Stroke="#1f1f1f"
        StrokeThickness=".3"
        X1="30"
        X2="30"
        Y1="0"
        Y2="1500" />
    <Line
        x:Name="ln5"
        Stroke="#1f1f1f"
        StrokeThickness=".3"
        X1="40"
        X2="40"
        Y1="0"
        Y2="1500" />
</Grid>

相关问题