C# UWP如何创建承载内容的用户控件?
我在试图实现一件非常琐碎的事情(或者至少,我所期望的事情应该是琐碎的…)时感到非常沮丧 我有一个需要定制切换按钮的需求,为此我需要创建一个承载切换按钮的用户控件,并承载该用户控件中描述的内容。我制作了一个小型应用程序来演示“需求” 代码隐藏:C# UWP如何创建承载内容的用户控件?,c#,uwp,controltemplate,C#,Uwp,Controltemplate,我在试图实现一件非常琐碎的事情(或者至少,我所期望的事情应该是琐碎的…)时感到非常沮丧 我有一个需要定制切换按钮的需求,为此我需要创建一个承载切换按钮的用户控件,并承载该用户控件中描述的内容。我制作了一个小型应用程序来演示“需求” 代码隐藏: public static DependencyProperty MainContentProperty = DependencyProperty.Register( "MainContent", typeof(ob
public static DependencyProperty MainContentProperty = DependencyProperty.Register(
"MainContent",
typeof(object),
typeof(MyUserControl1),
null);
public object MainContent
{
get => GetValue(MainContentProperty);
set => SetValue(MainContentProperty, value);
}
当我运行应用程序时,会显示文本,但样式/切换按钮被忽略/未应用/无论什么
视觉树确认我做错了什么:
我已经看过许多其他相关的SO Q&A,但我仍然不知道如何让它以我想要的方式工作。您的代码应该工作,除了没有显示应该在哪里的行。您能否确保
MyUserControl1
已识别其内容属性,并查看这是否有帮助
[ContentProperty(Name = "MainContent")]
public sealed partial class MyUserControl1 : UserControl
...
更新 下面是使用Win 10 Pro 1803、build 17134和NETCore 6.2.2测试的完整代码 请注意,您可以在
UserControl.Resources
或外部资源中定义控件模板,以将其与“主”UI布局分离,或将其保留在ToggleButton.template中,以减少XAML的几行
UserControlWithContent.xaml
UserControlWithContentPage.xaml
只是一个例子
页面XAML设计器屏幕截图
好吧,既然基于您的代码的方法对我有效,我将在这里发布完整的代码,以防万一它可能会对某人有所帮助。哈!一点一点地复制,这在我的电脑上也能正常工作。当时我使用的风格方法似乎不起作用。您的方式我也不必(/不允许)用
标记定义主页中的内容。无论如何,这项工作做得很好!Thx,那我下次再用:)
public static DependencyProperty MainContentProperty = DependencyProperty.Register(
"MainContent",
typeof(object),
typeof(MyUserControl1),
null);
public object MainContent
{
get => GetValue(MainContentProperty);
set => SetValue(MainContentProperty, value);
}
[ContentProperty(Name = "MainContent")]
public sealed partial class MyUserControl1 : UserControl
...
<UserControl
x:Class="SmallTests2018.UserControlWithContent"
x:Name="Self"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ToggleButton>
<ToggleButton.Template>
<ControlTemplate>
<Grid>
<Ellipse Width="300" Height="300" Fill="Blue"/>
<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{Binding MainContent, ElementName=Self, FallbackValue='{}{ content }'}" />
</Grid>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
</UserControl>
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
namespace SmallTests2018
{
[ContentProperty(Name = "MainContent")]
public sealed partial class UserControlWithContent : UserControl
{
public UserControlWithContent()
{
this.InitializeComponent();
}
public static DependencyProperty MainContentProperty =
DependencyProperty.Register("MainContent", typeof(object), typeof(UserControlWithContent), null);
public object MainContent
{
get => GetValue(MainContentProperty);
set => SetValue(MainContentProperty, value);
}
}
}
<Page
x:Class="SmallTests2018.UserControlWithContentPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SmallTests2018"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Viewbox>
<local:UserControlWithContent>
<TextBlock FontSize="32" Foreground="Yellow">Just an example</TextBlock>
</local:UserControlWithContent>
</Viewbox>
</Page>