Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在运行时动态更改App.xaml资源_Xaml_Uwp - Fatal编程技术网

如何在运行时动态更改App.xaml资源

如何在运行时动态更改App.xaml资源,xaml,uwp,Xaml,Uwp,我的应用程序的某些区域会根据我从web应用程序获得的颜色代码进行着色。我想了很多关于如何做到这一点,我不知道如何解决这个问题 我的第一个想法是编写一个ValueConverter,它只提供那些颜色,这样我就可以在任何时候调试它 但是现在我试图在一些样式上使用它,我遇到了一个问题,我无法在App.xaml中使用ValueConverter 因此,我试图存档的是,当我导航到mainpage.xaml这样的页面时,如果没有显示特定于用户的内容,我希望使用我公司的颜色,但是当用户导航到特定于用户的页面时

我的应用程序的某些区域会根据我从web应用程序获得的颜色代码进行着色。我想了很多关于如何做到这一点,我不知道如何解决这个问题

我的第一个想法是编写一个
ValueConverter
,它只提供那些颜色,这样我就可以在任何时候调试它

但是现在我试图在一些样式上使用它,我遇到了一个问题,我无法在
App.xaml
中使用
ValueConverter

因此,我试图存档的是,当我导航到mainpage.xaml这样的页面时,如果没有显示特定于用户的内容,我希望使用我公司的颜色,但是当用户导航到特定于用户的页面时,我希望显示当前用户的公司颜色

所以我在stackoverflow上搜索了如何实现这一点,我发现了这篇文章。但我总是在输出窗口中收到绑定失败的消息

<Setter Property="Background">
    <Setter.Value>
        <Binding Path="Background" RelativeSource="{RelativeSource Self}" TargetNullValue="a" FallbackValue="a">
            <Binding.Converter>
                <provider:DarkBackgroundProvider/>
            </Binding.Converter>
        </Binding>
    </Setter.Value>
</Setter>

有什么想法或者其他方法吗


我还阅读了一些网站上的内容,我可以替换
App.xaml
中的值,但它对我不起作用,因为每当我想设置
App.Resources[]=Color
的值时,我都会遇到异常情况。我查看了您的绑定数据错误,如下所示:

Exception thrown: 'System.FormatException' in PresentationCore.dll
System.Windows.Data Error: 12 : TargetNullValue 'a' (type 'String') cannot be converted for use in 'Background' (type 'Brush'). BindingExpression:Path=Background; DataItem=null; target element is 'Button' (Name=''); target property is 'Background' (type 'Brush') FormatException:'System.FormatException: Token is not valid.
绑定数据错误不是
DarkBackgroundProvider
的问题,而是
TargetNullValue
FallbackValue
的问题

您应该为以下两个属性创建有效值:

<Setter Property="Background">
    <Setter.Value>
        <Binding Path="Background" RelativeSource="{RelativeSource Self}">
            <Binding.TargetNullValue>
                <SolidColorBrush Color="White" />
            </Binding.TargetNullValue>
            <Binding.FallbackValue>
                <SolidColorBrush Color="White" />
            </Binding.FallbackValue>
            <Binding.Converter>
                <local:DarkBackgroundProvider />
            </Binding.Converter>
        </Binding>
    </Setter.Value>
</Setter>

这对我不起作用,它以白色显示,而不是从provider@MelloPs当然,它对您不起作用,因为您永远无法将属性绑定到属性本身,它肯定会失败,这就是您失败的真正原因。但是你问了另一个问题,你得到了一个绑定错误,你想解决它。我将更新我的答案以解决您的原始问题。@MelloPs建议编辑您的问题,以发布您的原始需求,您希望从提供商处设置
按钮
样式的背景。好的,我不知道这会有什么不同,因为我认为应用样式时,值转换器会得到值我想我只需要这个我会试试这个我可以在运行时更改值吗?因为具体的问题在于每个用户都有自己的颜色定制,并且用户可以来自不止一个Web应用程序,所以当用户登录时需要更改颜色
<Application x:Class="Walterlv.Styles.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Walterlv.Styles"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <SolidColorBrush x:Key="Brush.MainButton.Background" Color="White" />
        <Style TargetType="Button">
            <Setter Property="Background" Value="{DynamicResource Brush.MainButton.Background}" />
        </Style>
    </Application.Resources>
</Application>
public partial class App : Application
{
    protected override async void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        await Task.Delay(2000);

        Resources["Brush.MainButton.Background"] = new SolidColorBrush(Color.FromRgb(0x00, 0x7a, 0xcc));
    }
}