You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinUI3打包项目中如何将独立XAML线网格文件整合到主窗口

WinUI3 整合垂直/水平网格线到MainWindow的可行方案

方案一:将网格线封装为UserControl(最直接)

1. 改造V_Grid.xaml为UserControl

把原有Line包裹在UserControl和Canvas(用于绝对定位)中:

<UserControl
    x:Class="YourProjectNamespace.V_Grid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Canvas>
        <Line X1="0" X2="0" Y1="0" Y2="1500" Stroke="#1f1f1f" StrokeThickness=".3" />
        <Line X1="10" X2="10" Y1="0" Y2="1500" Stroke="#1f1f1f" StrokeThickness=".3" />
        <Line X1="20" X2="20" Y1="0" Y2="1500" Stroke="#1f1f1f" StrokeThickness=".3" />
        <!-- 其余垂直线 -->
    </Canvas>
</UserControl>

2. 同理改造H_Grid.xaml为UserControl

<UserControl
    x:Class="YourProjectNamespace.H_Grid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Canvas>
        <Line Y1="0" Y2="0" X1="0" X2="2000" Stroke="#1f1f1f" StrokeThickness=".3" />
        <Line Y1="10" Y2="10" X1="0" X2="2000" Stroke="#1f1f1f" StrokeThickness=".3" />
        <Line Y1="20" Y2="20" X1="0" X2="2000" Stroke="#1f1f1f" StrokeThickness=".3" />
        <!-- 其余水平线 -->
    </Canvas>
</UserControl>

3. 在MainWindow.xaml中引用UserControl

添加命名空间后直接嵌入两个UserControl:

<Window
    x:Class="YourProjectNamespace.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:YourProjectNamespace">
    <Grid>
        <!-- 垂直线在下层,水平线在上层,交叉显示 -->
        <local:V_Grid />
        <local:H_Grid />
    </Grid>
</Window>

替换YourProjectNamespace为你实际的项目命名空间。


方案二:用ResourceDictionary存储线集合,通过ItemsControl加载

1. 改造V_Grid.xaml为ResourceDictionary

将垂直线存入数组资源:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <x:Array x:Key="VerticalLines" Type="Line">
        <Line X1="0" X2="0" Y1="0" Y2="1500" Stroke="#1f1f1f" StrokeThickness=".3" />
        <Line X1="10" X2="10" Y1="0" Y2="1500" Stroke="#1f1f1f" StrokeThickness=".3" />
        <!-- 其余垂直线 -->
    </x:Array>
</ResourceDictionary>

2. 同理改造H_Grid.xaml为ResourceDictionary

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <x:Array x:Key="HorizontalLines" Type="Line">
        <Line Y1="0" Y2="0" X1="0" X2="2000" Stroke="#1f1f1f" StrokeThickness=".3" />
        <Line Y1="10" Y2="10" X1="0" X2="2000" Stroke="#1f1f1f" StrokeThickness=".3" />
        <!-- 其余水平线 -->
    </x:Array>
</ResourceDictionary>

3. 在App.xaml中合并资源字典

<Application
    x:Class="YourProjectNamespace.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                <ResourceDictionary Source="V_Grid.xaml" />
                <ResourceDictionary Source="H_Grid.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

4. 在MainWindow.xaml中加载线集合

用Canvas承载,通过ItemsControl绑定资源数组:

<Window
    x:Class="YourProjectNamespace.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Canvas>
            <ItemsControl ItemsSource="{StaticResource VerticalLines}" />
            <ItemsControl ItemsSource="{StaticResource HorizontalLines}" />
        </Canvas>
    </Grid>
</Window>

为什么之前的方法无效

  • 直接用<Page>标签加载Line集合错误:Page是独立页面容器,不是用于嵌入UI元素集合的控件。
  • 在资源字典中定义Page类型资源无意义:资源字典用于存储可复用资源(如样式、模板、数据),而非页面实例。

内容的提问来源于stack exchange,提问作者jv1597

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 06:09:56