如何在 C# ContentPage 中访问 App.xaml 上的静态资源模板?

大卫·扎佐索·莫雷诺

如何在 C# ContentPage 中访问 App.xaml 上的静态资源模板?

这是我的代码。App.xaml:

 <Application xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" >
    <Application.Resources>
        <ControlTemplate x:Key="template">
             ...
             <Label Text="{Binding lbl_title}" Grid.Row="0"/>
                        
             <!--Body -->
             <ContentPresenter Grid.Row="1"/>
             ...
        </ControlTemplate>
    </Application.Resources>
</Application>

ContentPage.xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             ControlTemplate="{StaticResource template}">

    <Grid x:Name="body_grid"/>
</ContentPage>

和 ContentPage.cs:

public partial class MenuPage : ContentPage{
   public MenuPage() {
       lbl_title.Text = "Title"; //This throw me and ERROR
       ... // Grid definition
   }
}

我需要访问 lbl_title 对象,而不仅仅是他的 .Text 属性,还有他的颜色、背景等......

王凯尔 - MSFT

这是控制模板:

<ControlTemplate x:Key="template">
    <StackLayout>
        <Label x:Name="lbl_title" Text="test"/>
        <Button />
    </StackLayout>
</ControlTemplate>

要获取 ControlTemplate 中的元素,可以尝试将页面IPageController强制转换并使用 foreach 遍历其包含的控件。

主页.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        var controller = this as IPageController;
        foreach (var child in controller.InternalChildren)
        {
            var result = child.FindByName<Label>("lbl_title");
            if (result != null)
            {
                Label myControl = result;
                myControl.Text = "new text";
                myControl.BackgroundColor = Color.Red;
            }
        }
    }
    // ...
}

更新:

或者调用TemplatedPage.GetTemplateChild(String) 方法此方法用于“检索实例化的 ControlTemplate 可视化树中的命名元素”。

Label myControl = this.GetTemplateChild("lbl_title") as Label;
myControl.Text = "new test text";

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何以编程方式从App.xaml访问资源字符串?

如何在C#中访问HubView XAML的子元素?

如何在Xamarin.Forms中设置ContentPage方向

如何在Xamarin形式的XAML中动态更改ContentPage标题?

Xamarin Forms嵌入资源图像作为XAML中ContentPage的背景

如何从C#中的资源字典(XAML)获取值

如何从Xamarin Forms中的C#中的App.xaml访问颜色?

Xamarin继承Xaml中的ContentPage

如何在contentpage的contentview的堆栈布局中添加控件?

如何在XAML中使用静态资源?

如何在单个App.xaml中合并不同的资源?

C#UWP如何在xaml中显示string []?

如何在C#中从ViewModelHelper关闭窗口(XAML)?

在XAML中处理静态资源

在C#中创建框架而不是XAML时,如何在构造函数中分配动态资源?

Xaml C#中可观察的集合静态资源

如何在XAML中将默认数据模板指定为静态资源

Windows应用商店-XAML C#-如何在代码中访问Page.Resource

更新通用Windows App XAML中的资源

如何使用XAML访问模板中的命名控件

如何在 Xamarin 中自定义 ContentPage 标题?

如何在 XAML 中访问父模板样式的元素?

如何从 Xamarin Forms 的 ContentPage 保存 App.xaml 中的资源?

如何使用 C# 后端而不是在 XAML 中绑定模板上的 XAML 元素?

如何从 C++/CX 中的 XAML 资源获取 CommandBar?

如何在 App.xaml 静态资源中使用嵌套字符串...?

如何在 C# 中为 ContentPage 设置 Shell.NavBarIsVisible="false"?

如何在 C# 中命名 ContentPage?

如何使用 c# 而不是 xaml 创建 .Net Maui ContentPage?