[英]color in xaml from c#(windows phone)
XAML:
<Rectangle Fill="{Binding Gray}" />
C#:
public class colors
{
public string Gray
{
set {}
get{ return "#FF22262a";}
}
}
編譯時沒有錯誤。 但是矩形沒有填寫“#FF22262a”
編輯:此代碼也不起作用:
MainPage.xaml中
<TextBlock Text="{Binding MyGray2}"></TextBlock>
MainPage.xaml.cs中
public String MyGray2
{
set { }
get { return "gjnuegheugheog"; }
}
EDIT2:
<phone:PhoneApplicationPage
x:Class="FlagLib.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:FlagLib"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
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 Grid.Column="0" Tap="onToggleHorizontal" DataContext="{StaticResource vmColors}">
<Rectangle Fill="{Binding Gray}" />
...
EDIT3:
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>
colors.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"; }
}
}
}
為什么我得到名稱“colors”在命名空間“clr-namespace:proba5”中不存在。
您需要設置DataContext
。
public MyPage()
{
DataContext = new colors();
}
我更喜歡在XAML中這樣做。
在頂部導入您的命名空間
xmlns:vm="clr-namespace:MyNamespace"
將該類添加為資源(如果您願意,可以在應用程序級別執行此操作)
<phone:PhoneApplicationPage.Resources>
<vm:colors x:Key="vmColors" />
</phone:PhoneApplicationPage.Resources>
分配給DataContext(在這種情況下,我已將其分配給Grid,因此Grid中的任何控件都將采用vmColors
DataContext,除非您為該特定子控件更改它)。
<Grid DataContext="{StaticResource vmColors}">
<Rectangle Fill="{Binding Gray}" />
</Grid>
這是頁面的完整XAML,因此您可以看到代碼的位置。
<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>
你不能這樣做。 你想要的是隱式地將一個string
轉換為Color
,這是不可能的。 你應該更好地考慮這段代碼:
public SolidColorBrush MyGray
{
set { }
get { return new SolidColorBrush(Color.FromArgb(172, 172, 172, 0)); }
}
這是我的來源: http : //msdn.microsoft.com/fr-fr/library/system.windows.shapes.shape.fill.aspx
在XAML中還有其他管理顏色的方法,您可以瀏覽msdn以獲取更多信息。
編輯:編譯時沒有錯誤是正常的。 XAML在執行時被解釋。
編輯2:更新代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.