从代码隐藏中的子文件夹获取特定的XAML资源文件

问题描述

我正在尝试在代码中使用Style,并且在后面的代码中将其设置为特定的resource file,这应该是相对容易完成的事情,但是很遗憾,我已经花费了2多个小时实现它。

我的Xamarin.Forms ContentPage中具有以下XAML代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="PCS2.APP.HomePage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
    xmlns:fftransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations"
    xmlns:grial="clr-namespace:UXdivers.Grial;assembly=UXdivers.Grial"
    xmlns:local="clr-namespace:PCS2.APP"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Shell.NavBarIsVisible="False">

    <ContentPage.Resources>
        <ResourceDictionary Source="/Styles/TabControl/IOSTabResources.xaml" />
    </ContentPage.Resources>
    
    <ContentPage.Content>
        <grial:TabControl
                x:Name="tabControl"
                Style="{StaticResource iOSTabStyle}"
                TabContentBackgroundColor="{DynamicResource BasePageColor}"
                TabStripBackgroundColor="{DynamicResource ArticleHeaderBackgroundColor}">
        </grial:TabControl>
    
    </ContentPage.Content>
</ContentPage>

但是,我想从后面的代码构建我的组件(TabControl)。因此,我从XAML页面删除TabControl,并在视图/页面的C#中添加了以下代码

public partial class HomePage : ContentPage
{
   
    public HomePage()
    {
        InitializeComponent();
        
        var tabControl = new TabControl
        {
            Style = (Style)Application.Current.Resources["iOSTabStyle"],TabContentBackgroundColor = (Color)Application.Current.Resources["BasePageColor"],TabStripBackgroundColor = (Color)Application.Current.Resources["ArticleHeaderBackgroundColor"]
        };
        
        Content = tabControl;
    }
    
}

但是,在设置静态资源样式Style = (Style)Application.Current.Resources["iOSTabStyle"]时,我收到了错误'Can't find Resource ...'

通常,Application.Current.Resources可以正常工作,但是我注意到在原始XAML文件中,特定的资源文件(其中具有键/样式iOSTabStyle)是单独加载的:

<ContentPage.Resources>
    <ResourceDictionary Source="/Styles/TabControl/IOSTabResources.xaml" />
</ContentPage.Resources>

特定资源文件Embedded resource文件中未引用/未包含的App.xaml

问题是,如何访问特定的资源文件及其内容/ C#中的键/后面的代码

我已经尝试了很多类似以下的事情:

var myResource = Resources.LoadFromXaml("/Styles/TabControl/IOSTabResources.xaml");

但没有成功。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)