绑定Usercontrol的datacontext

时间:2015-02-04 02:17:50

标签: c# windows-phone-8 mvvm datacontext

我是从wpf app开始的。我正在尝试练习MVVM模式。我无法绑定Usercontrol的datacontext。

<UserControl x:Class="Test.Views.Login.Identifer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Test.ViewModels"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">

<UserControl.DataContext>
    <vm:Login/>
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <TextBox Name="user"></TextBox>
    <TextBox Grid.Row="1" Name="password"></TextBox>
</Grid>

这是我的usercontrol,Login是我想绑定到我的datacontext的类。我把这个usercontrol放在我的PhonApplicationPage中:

<phone:PhoneApplicationPage
x:Class="Test.Views.Login.Login"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Views="clr-namespace:Test.Views.Login"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot est la grille racine où tout le contenu de la page est placé-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Views:Identifer></Views:Identifer>
</Grid>

我将使用我的登录课来完成finsih:

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

namespace Test.ViewModels
{
class Login : MyNotifyPropertyChanged
{
    public Client client_ { get; set; }
    public Login()
    {
        client_ = new Client();
    }
}
}

我遇到以下错误: Erreur 1无法创建“Test.ViewModels.Login”类型的实例[行:14位置:19]

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你的班级应该公开时是私人的

public class Login : MyNotifyPropertyChanged //add public keyword