【问题标题】:Reutilize CommandBar UWP重新利用 CommandBar UWP
【发布时间】:2017-09-19 20:25:16
【问题描述】:

我在几个页面中使用了 CommandBar。 我注意到我在这部分代码中重复了自己:

<CommandBar  x:Name="MyCommandBar">
    <CommandBar.Content>
        <TextBlock Text="{Binding MyTitle}"/>
    </CommandBar.Content>
</CommandBar>

我尝试创建这样的自定义控件,然后根据页面添加新的buttons

<UserControl
    x:Class="TutorialManager.UWP.Views.Controls.MainCommandBarControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    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"
    d:DesignHeight="300"
    d:DesignWidth="400">


    <CommandBar >
        <CommandBar.Content>
            <TextBlock Text="{Binding MyTitle}"/>
        </CommandBar.Content>
    </CommandBar>
</UserControl>

问题是,例如,当我在此自定义控件中添加一个新按钮时,它会与第一个按钮重叠。

有解决办法吗?

-- 编辑 1 --
第一个解决方案看起来不错,但是当我添加一个按钮时,它会进入底部:

<controls:CustomCommandBarControl Title="{Binding TituloPagina}" Foreground="WhiteSmoke">

    <AppBarButton Icon="Accept" Label="Save"  Foreground="White" />

</controls:CustomCommandBarControl>

-- 编辑 2 --
看起来更好,但不幸的是我仍然遇到了一些问题。

  1. 我添加的新按钮与more 按钮不对齐(甚至设置属性)
  2. 新按钮始终显示其标签。
  3. More 按钮在前后导航时会自行隐藏。
  4. 看起来我的Custom Bar继承了以前导航栏中的一些属性

【问题讨论】:

    标签: uwp xamarin.uwp


    【解决方案1】:

    您可以创建一个新的 TemplatedControl 并创建一个像这样的类:

    [ContentProperty(Name = "Content")]
    public sealed class CustomUserControl : Control
    {
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
    
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register(nameof(Title), typeof(string), typeof(CustomUserControl), new PropertyMetadata(string.Empty));
    
    
        public ObservableCollection<UIElement> Content
        {
            get { return (ObservableCollection<UIElement>)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }
    
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register(nameof(Content), typeof(ObservableCollection<UIElement>), typeof(CustomUserControl), new PropertyMetadata(null));
    
        public CustomUserControl()
        {
            Content=new ObservableCollection<UIElement>();
            this.DefaultStyleKey = typeof(CustomUserControl);
        }
    }
    

    在您的 generic.xaml 中定义与此类似的控件(根据您的需要添加边距等):

    <Style TargetType="local:CustomUserControl" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:CustomUserControl">
                    <CommandBar VerticalAlignment="Bottom" >
                        <CommandBar.Content>
                            <StackPanel Orientation="Horizontal"
                                        Margin="4,6,4,4">
                                <TextBlock Text="{TemplateBinding Title}"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Top"
                                           Margin="4,12,0,0" />
                                <ItemsControl ItemsSource="{TemplateBinding Content}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel Orientation="Horizontal"/>
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                </ItemsControl>
                            </StackPanel>
                        </CommandBar.Content>
                    </CommandBar>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    快速构建后,您可以以非常简单的方式使用您的新控件:

    <yourNamespace:CustomUserControl Title="{Binding MyTitle}">
        <Button Content="Command1"/>
        <Button Content="Command2" />
    </yourNamespace:CustomUserControl>
    

    【讨论】:

    • 感谢您提出问题,我已编辑问题以在使用您的解决方案时添加图像。你知道怎么解决吗?
    • 我改变了 StackPanel 的方向。这应该可以解决问题。
    • 差不多了,看我的编辑2,希望你能再次帮助我。
    • 我稍微编辑了一下,但我不知道你想要什么,所以我不确定这是否是你想要的。但是,您可以自己修改代码,直到它适合您的需要为止。
    • 你说得对,这非常困难,因为 CommandBar 的generic.xaml 具有广泛的上下文。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多