将ViewModel绑定到MainPage.xaml

时间:2011-12-19 12:30:59

标签: .net silverlight-4.0 mvvm

我在MainPage.xaml.cs中有以下代码

public partial class MainPage : UserControl
    {
        private IsolatedStorageSettings userLogin = IsolatedStorageSettings.ApplicationSettings;
        private UserDataContract currentUser;
        private MainViewModel mainViewModel;

        public MainPage()
        {
            InitializeComponent();
        }

        // If an error occurs during navigation, show an error window
        private void ContentFrameNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Navigate(new Uri("#/Fields/Edit", UriKind.Relative));
        }

    }

现在我想将我的ViewModel绑定到MainPage.xaml,我该怎么做?

            mainViewModel = new MainViewModel();
            this.LayoutRoot.DataContext = loginViewModel;

不起作用

0 个答案:

没有答案