【问题标题】:Set value of x:Uid programmatically以编程方式设置 x:Uid 的值
【发布时间】:2017-05-13 13:55:16
【问题描述】:

我正在 XAML 视图的代码隐藏文件中创建一个 AppBarButton。 目前,我在 XAML 中定义了 AppBarButton,如下所示:

<AppBarButton x:Name="SelectVisitAppBarButton" x:Uid="AppBar_TransferVisit"  Margin="0,0,12,0" Icon="Bullets" Style="{StaticResource TransferAppBarButtonStyle}" IsEnabled="{Binding ScheduleViewVm.IsVisitSelectionEnable}" Click="SelectVisit_Click" />    

我想将此 XAML 转换为 C# 代码。以下是我到现在为止的代码。

AppBarButton SelectVisitAppBarButton = new AppBarButton();    
SelectVisitAppBarButton.Margin = new Thickness(0, 0, 12, 0);
SymbolIcon bulletSymbol = new SymbolIcon();
bulletSymbol.Symbol = Symbol.Bullets;
SelectVisitAppBarButton.Icon = bulletSymbol;
SelectVisitAppBarButton.Style = App.Current.Resources["TransferAppBarButtonStyle"] as Style;
SelectVisitAppBarButton.Click += SelectVisit_Click;
Binding b = new Binding();
b.Source = _viewModel.ScheduleViewVm.IsVisitSelectionEnable;
SelectVisitAppBarButton.SetBinding(Control.IsEnabledProperty, b);
Appbar.Children.Add(SelectVisitAppBarButton);

我唯一要寻找的是将 x:Uid="AppBar_TransferVisit" 转换为其等效的 C# 代码。对此的任何帮助都非常感谢。

谢谢, 纳雷什·拉夫拉尼。

【问题讨论】:

    标签: c# xaml uwp winrt-xaml windows-mobile


    【解决方案1】:

    您似乎无法在 UWP 中以编程方式实际设置 x:Uid 属性:

    How to set control x:Uid attribute programmatically for a metro app control?

    这是因为它是 XAML 指令而不是属性:

    Doing localization in visualstatemanager by changing x:uid?

    您必须直接设置 AppBarButton 的相应属性,例如:

    AppBarButton SelectVisitAppBarButton = new AppBarButton();
    var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
    var text = resourceLoader.GetString("SomeResource");
    AppBarButton SelectVisitAppBarButton = new AppBarButton();
    SelectVisitAppBarButton.Content = text;
    

    【讨论】:

      【解决方案2】:

      我可以确认我已与 UWP-XAML 平台团队讨论过以编程方式访问 x:UID,但这是不可能的。如果你问我,这是一个缺点。但这就是现在的情况。 :)

      【讨论】:

      • 如果您能分享我能找到的任何文档链接,将会很有帮助。
      【解决方案3】:

      有一种解决方法,使用 XamlReader 从 xaml 加载按钮。

       Button btn = (Button)XamlReader.Load("<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Uid='btnSubmit' Margin='5,0,0,0'/>");
       btn.OtherProperty = xxxx
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-14
        • 1970-01-01
        • 1970-01-01
        • 2018-07-25
        • 2014-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多