简体   繁体   中英

The name does not exist in the namespace

I try to set color of rectangle from C# code. In other topic help me to use DataContext but can not bind class colors and key 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>

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"; }
        }
    }
}

Why i got The name "colors" does not exist in the namespace "clr-namespace:proba5".

将colors.cs“复制到输出目录”的属性更改为“始终复制”,它的工作正常!

Sometimes Visual Studio is not able to resolve names in XAML files if there are other compilation errors in the project or solution.

Try to fix all the other errors (and warnings). Then do a rebuild.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM