我尝试从 C# 代码设置矩形的颜色。在其他主题中帮助我使用 DataContext 但无法绑定类颜色和键 vmColors
MainPage.xaml:
<phone:PhoneApplicationPage
x:Class="proba5.MainPage"
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:vm="clr-namespace:proba5"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<vm:colors x:Key="vmColors"/>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid DataContext="{StaticResource vmColors}">
<Rectangle Fill="{Binding Gray}" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
颜色.cs
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"; }
}
}
}
为什么我得到 命名空间“clr-namespace:proba5”中不存在名称“colors”。