c#(windows phone)中的xaml颜色

c#(windows phone)中的xaml颜色,c#,windows-phone,C#,Windows Phone,XAML: 编译时没有错误。但矩形不填充“#FF22262a” 编辑: 此代码也不起作用: MainPage.xaml public class colors { public string Gray { set {} get{ return "#FF22262a";} } } 编辑2: public String MyGray2 { set { } get { return "gjnuegheugheog"; } }

XAML:

编译时没有错误。但矩形不填充“#FF22262a”

编辑: 此代码也不起作用:
MainPage.xaml

public class colors
{
    public string Gray
    {
        set {}
        get{ return "#FF22262a";}
    }
}
编辑2:

public String MyGray2
{
    set { }
    get { return "gjnuegheugheog"; }
}
为什么我会
名称空间“clr namespace:proba5”中不存在名称“colors”。

您不能这样做。您试图将
字符串
隐式转换为
颜色
,这是不可能的。您最好考虑以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proba5
{
    public class colors
    {
        public string Gray
        {
            set { }
            get { return "#FF22262a"; }
        }
    }
}
以下是我的资料来源:

在XAML中还有其他管理颜色的方法,您可以浏览msdn以获取更多信息

编辑:编译时没有错误是正常的。XAML在执行时被解释


编辑2:更新代码。

您需要设置
DataContext

public SolidColorBrush MyGray
{
    set { }
    get { return new SolidColorBrush(Color.FromArgb(172, 172, 172, 0)); }
}
我更喜欢用XAML来做

在顶部导入命名空间

public MyPage()
{
   DataContext = new colors();
}
将类添加为资源(如果愿意,可以在应用程序级别执行此操作)

这是一个页面的完整XAML,您可以看到代码的去向

<Grid DataContext="{StaticResource vmColors}">
   <Rectangle Fill="{Binding Gray}" />
</Grid>

你浏览过我刚才给你看的链接吗?我更新了我的答案,以便更明确地告诉您。现在我遇到了错误:无法将System.Windows.Media.SolidColorBrush类型隐式转换为System.Windows.Media.Color。返回值可能应该是
Brush
而不是
Color
有一个内置转换器,可以将字符串转换为Brush。无需更改OP的代码。当您编写
Fill=“FF22262a”
时,它是否有效?您是否收到任何绑定错误?它与
Fill=“#FF22262a”
一起工作。您是否在调试输出窗口中收到任何绑定错误?您是否设置了
DataContext
?作为旁注,我可能会将
colors
作为
ResourceDictionary
放在
StaticResource
中,并将其保存在xaml中。您的绑定需要DataContext,否则它不知道从何处绑定(它如何知道“Gray”属性在何处?)。所以您想告诉它,“查看我的
colors
类中我所做的任何绑定调用”。您可以在
页面
的代码隐藏中(例如MainPage.xaml)或在实际的xaml中执行此操作。这就是
DataContext
属性的基本功能。键入时正在更新答案:)您也可以执行
操作并跳过
网格中的
DataContext=“
”默认值,这同样有效。我故意这样做,以便OP可以看到
StaticResource
用于其他目的,但您的建议很好。@YaroslavShabanov-您在XAML中使用了
MyGray
,但该属性名为
Gray
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace proba5
{
    public class colors
    {
        public string Gray
        {
            set { }
            get { return "#FF22262a"; }
        }
    }
}
public SolidColorBrush MyGray
{
    set { }
    get { return new SolidColorBrush(Color.FromArgb(172, 172, 172, 0)); }
}
public MyPage()
{
   DataContext = new colors();
}
xmlns:vm="clr-namespace:MyNamespace"
<phone:PhoneApplicationPage.Resources>
    <vm:colors x:Key="vmColors"  />
</phone:PhoneApplicationPage.Resources>
<Grid DataContext="{StaticResource vmColors}">
   <Rectangle Fill="{Binding Gray}" />
</Grid>
<phone:PhoneApplicationPage
    x:Class="MyClass.MyPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:vm="clr-namespace:MyNamespace" // << IMPORT NAMESPACE
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d">


    //ALLOW THE CLASS TO BE ACCESSED VIA STATICRESOURCE
    <phone:PhoneApplicationPage.Resources>
        <vm:colors x:Key="vmColors"/> 
    </phone:PhoneApplicationPage.Resources>


    <Grid x:Name="LayoutRoot" Background="Transparent">      

        //SET THE DATACONTEXT OF THE GRID TO THE COLORS CLASS
        <Grid DataContext="{StaticResource vmColors}">
            <Rectangle Fill="{Binding Gray}" />
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>