Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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
C# Xamarin持久颜色选择器_C#_Xamarin_Xamarin.forms_Get_Set - Fatal编程技术网

C# Xamarin持久颜色选择器

C# Xamarin持久颜色选择器,c#,xamarin,xamarin.forms,get,set,C#,Xamarin,Xamarin.forms,Get,Set,-有我做什么的照片 你好, 我为xamarin.forms应用程序制作了一个按钮和颜色选择器,但当我选择一种颜色(例如红色)并关闭应用程序时,当我重新打开该应用程序以查看自动拾取的红色时,我想创建该按钮和颜色选择器。我尝试使用此代码,但首选项不适用于颜色: public Color ColorPicker { get => Preferences.Get(nameof(ColorPicker), color.Red); set { Preferenc

-有我做什么的照片

你好,

我为xamarin.forms应用程序制作了一个按钮和颜色选择器,但当我选择一种颜色(例如红色)并关闭应用程序时,当我重新打开该应用程序以查看自动拾取的红色时,我想创建该按钮和颜色选择器。我尝试使用此代码,但首选项不适用于颜色:

public Color ColorPicker
{
    get => Preferences.Get(nameof(ColorPicker), color.Red);
    set
    {
        Preferences.Set(nameof(ColorPicker), value);
        OnPropertyChanged(nameof(ColorPicker));
    }
}

有人能帮我吗?

您可以将Xamarin.Forms.Color存储为如下字符串:

public string ColorPicker
{
    get => Preferences.Get(nameof(ColorPicker), Color.Red.ToString());
    set
    {
        Preferences.Set(nameof(ColorPicker), value);
        OnPropertyChanged(nameof(ColorPicker));
    }
}
<Label TextColor="{Binding ColorPicker}" />
然后您可以将其绑定到标签,例如:

public string ColorPicker
{
    get => Preferences.Get(nameof(ColorPicker), Color.Red.ToString());
    set
    {
        Preferences.Set(nameof(ColorPicker), value);
        OnPropertyChanged(nameof(ColorPicker));
    }
}
<Label TextColor="{Binding ColorPicker}" />


确保在视图中设置了BindingContext。您可以阅读有关绑定的更多信息

我不可能这样做。因为我需要在转换后使用converter来生成string=>color。我正在尝试:

    public class StringToColor : IValueConverter
{
    ColorTypeConverter converter = new ColorTypeConverter();
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //return value.ToString(); //not working
        //return (Color)(converter.ConvertFromInvariantString(value.ToString())); //not working
        return Color.FromHex(value.ToString()); //not working too
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}
并将此转换器添加到xaml

<ContentPage.Resources>
    <ResourceDictionary>
        <local:StringToColor x:Key="str" />
    </ResourceDictionary>
</ContentPage.Resources>


    <Label Text="TEST" FontSize="Title" TextColor="{Binding ColorPicker,
    Converter={StaticResource str}}"/>

但是什么都没发生

Color.FromHex(字符串值)方法需要
string
类型参数。尝试将转换为自定义类中的字符串类型

检查代码:

自定义转换器类

public class StringToColorConverter : IValueConverter, INotifyPropertyChanged
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var color = Color.FromHex(value as string);
        return color;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Page.xaml

<ContentPage.Resources>
    <local:StringToColorConverter x:Key="myConverter" />
</ContentPage.Resources>


<ContentPage.Content>
    <StackLayout BackgroundColor="{Binding Color_string, Converter={StaticResource myConverter}}">
        ...
    </StackLayout>
</ContentPage.Content>

...

在Xamarin.Forms中,应用程序子类有一个静态属性字典,可用于存储数据。例如
Application.Current.Properties[“myColor”]=color
检查链接:
Color.FromHex(字符串值)
方法需要一个
string
类型参数。尝试在自定义类中将值转换为字符串类型。谢谢,现在它工作得很好,只需添加
get=>Preferences.get(nameof(Color_string),Color.Red.ToHex())现在的另一个问题是我无法设置新的颜色
设置{Preferences.set(nameof(color_string),value as string);OnPropertyChanged(nameof(color_string));}
我使用按钮更改背景颜色,但设置事件不会出现…似乎没有命令在“set”方法中为属性赋值。只有
首选项.Set
存在,用于在首选项中保存值。尝试添加
_属性=值代码。