首页 > 解决方案 > Avalonia Ui 将 UserControl 放入另一个

问题描述

我需要执行以下代码:

<UserControl x:Class="MyApp.Views.UserControls.UCMainControl" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
             xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:dxnav="http://schemas.devexpress.com/winfx/2008/xaml/navigation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Controls="clr-namespace:MyApp.Views.UserControls">

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
      <RowDefinition Height="140"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto"/>
      <ColumnDefinition/>
      <ColumnDefinition Width="220"/>
    </Grid.ColumnDefinitions>

    <Controls:UCMenuPrincipal x:Name="MenuPrincipalcontrol" Grid.Row="0"/>

  </Grid>

在这段代码中,我需要在另一个 UserControl 上放置一个菜单,但该菜单位于不同的 UserControl 中(但是,它可以是任何东西)。

当代码在正常的 WPF 中时,我可以做到。Avalonia 可以做到这一点吗?

标签: c#wpfavaloniauiavalonia

解决方案


不完全确定您要的是什么,但我认为您正在尝试使用内部带有菜单的用户控件,然后在窗口中使用菜单托管此用户控件。

像这样的东西应该工作:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:MyApp.ViewModels;assembly=MyApp"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:uc="clr-namespace:MyApp.Views.UserControls;assembly=MyApp"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="MyApp.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="MyApp">

    <uc:UCMainControl/>

</Window>
<UserControl xmlns="https://github.com/avaloniaui"
             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:uc="clr-namespace:MyApp.Views.UserControls;assembly=MyApp"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="MyApp.Views.UserControls.UCMainControl">

  <Grid RowDefinitions="Auto,Auto,*,140" ColumnDefinitions="Auto,*,220">
    <uc:UCMenuPrincipal x:Name="MenuPrincipalcontrol" Grid.Row="0"/>
  </Grid>

</UserControl>
<UserControl xmlns="https://github.com/avaloniaui"
             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"
             mc:Ignorable="d"
             x:Class="MyApp.Views.UserControls.UCMenuPrincipal">

  <Menu Background="AliceBlue">
    <MenuItem Header="File"/>
    <MenuItem Header="Edit"/>
    <MenuItem Header="Help"/>
  </Menu>

</UserControl>

结果窗口


推荐阅读