Xamarin按ID查找资源,然后按名称搜索

时间:2019-10-21 20:58:31

标签: c# xaml xamarin

我有这个跟随资源,我能够通过ID查找资源,但是我找不到一些如何通过名称查找子元素的方法

            <ControlTemplate x:Key="MainPageTemplate">
            <Grid
                BackgroundColor="White"
                HorizontalOptions="FillAndExpand"
                RowSpacing="0"
                VerticalOptions="FillAndExpand">
                <Grid.RowDefinitions>
                    <RowDefinition Height="50" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="50" />
                </Grid.ColumnDefinitions>

                <StackLayout
                    Grid.Row="0"
                    Grid.Column="0"
                    BackgroundColor="Transparent"
                    HeightRequest="50"
                    VerticalOptions="CenterAndExpand"
                    WidthRequest="50">
                    <ffimageloadingsvg:SvgCachedImage
                        Margin="10,0"
                        HeightRequest="24"
                        HorizontalOptions="StartAndExpand"
                        Source="resource://ABSCardApp.Resources.ic_menu.svg"
                        VerticalOptions="CenterAndExpand"
                        WidthRequest="24" />
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer Tapped="Button_Clicked" />
                    </StackLayout.GestureRecognizers>
                </StackLayout>
                <ffimageloadingsvg:SvgCachedImage
                    Grid.Row="0"
                    Grid.Column="1"
                    Margin="0,0,0,8"
                    HeightRequest="20"
                    X:Name="LogoIcon"
                    HorizontalOptions="StartAndExpand"
                    Source="resource://ABSCardApp.Resources.logo.svg"
                    VerticalOptions="CenterAndExpand"
                    WidthRequest="96" />
  ....

我正在使用以下跟踪代码来获取资源ID

var resource = Application.Current.Resources["MainPageTemplate"];

但是我对此感到不安,因为即使将结果转换为ResourceDictionaryControlTemplate,我也无法在此结果上使用FindByName

resource.FindByName("LogoIcon")

我不确定我是否做得正确。

1 个答案:

答案 0 :(得分:1)

正如jason所说,您必须将这些属性绑定到模型或视图模型上的属性 在模板中命名UI控件毫无意义。

例如,您在App.xaml中具有controltemplate,则应该对SvgCachedImage源使用绑定。

 <ControlTemplate x:Key="MainPageTemplate">
            <Grid BackgroundColor="White" VerticalOptions="Center">
                <ffimageloadingsvg:SvgCachedImage x:Name="LogoIcon" Source="{TemplateBinding BindingContext.Url}" />
                <ContentPresenter />

            </Grid>
        </ControlTemplate>

现在,在许多内容页面中,您应该为此分配不同的值。

<ContentPage
x:Class="demo2.dictionary.Page1"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentView
    x:Name="contentView"
    Padding="0,20,0,0"
    ControlTemplate="{StaticResource MainPageTemplate}">
    <StackLayout>
        <Label
            HorizontalOptions="CenterAndExpand"
            Text="Welcome to Xamarin.Forms!"
            VerticalOptions="CenterAndExpand" />

    </StackLayout>
</ContentView>

public partial  class Page1 : ContentPage, INotifyPropertyChanged
{
    private string _Url;
    public string Url
    {
        get { return _Url; }
        set
        {
            _Url = value;
            RaisePropertyChanged("Url");

        }
    }
    public Page1 ()
    {
        InitializeComponent ();
        Url = "a11.jpg";
        this.BindingContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

如果我的回复解决了您的问题,请记住将回复标记为答案,谢谢。