【问题标题】:The first time for MVVM and I got a error. So I did not know where is error第一次使用 MVVM,我遇到了错误。所以我不知道错误在哪里
【发布时间】:2015-04-09 13:21:50
【问题描述】:

App.cs {

#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page

                rootFrame = new Frame();
                var viewModel = new MainWindowModel();
                MainPage main = new MainPage();
                main.DataContext = viewModel;

                // TODO: change this value to a cache size that is appropriate for your application
                rootFrame.CacheSize = 1;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    // TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

DataAccess.cs

{
        readonly List<Menu> _menu;

        public MenuRespository()
        {
            if(_menu==null)
            {
                _menu = new List<Menu>();
            }
            _menu.Add(Menu.CreateMenu("/Photos/Gas-50.png", "t"));
            _menu.Add(Menu.CreateMenu("/Photos/Gas-50.png", "t"));
            _menu.Add(Menu.CreateMenu("/Photos/Gas-50.png", "t"));
            _menu.Add(Menu.CreateMenu("/Photos/Gas-50.png", "t"));
            _menu.Add(Menu.CreateMenu("/Photos/Gas-50.png", "t"));
            _menu.Add(Menu.CreateMenu("/Photos/Gas-50.png", "t"));
            _menu.Add(Menu.CreateMenu("/Photos/Gas-50.png", "t"));
        }
        public List<Menu> GetMenu()
        {
            return new List<Menu>(_menu);
        }
    }

菜单.cs

public class Menu
    {
        public static Menu CreateMenu(string iconthumb, string text)
        {
            return new Menu { IconThumb = iconthumb, Text = text };
        }
        public string IconThumb { get; set; }
        public string Text { get; set; }
    }

MainWindowsModel
 public class MainWindowModel :ViewModelBase
    {
        readonly MenuRespository _menuRepository;
        ObservableCollection<ViewModelBase> _viewModels;
        public MainWindowModel()
        {
            _menuRepository = new MenuRespository();
            //create an instance of our viewmodel add it to our collection
            MenuListViewModel viewModel = new MenuListViewModel(_menuRepository);
            this.ViewModels.Add(viewModel);
        }
        public ObservableCollection<ViewModelBase> ViewModels
        {
            get
            {
                if(_viewModels==null)
                {
                    _viewModels = new ObservableCollection<ViewModelBase>();

                }
                return _viewModels;
            }
        }
    }

视图库

 public abstract class ViewModelBase :INotifyPropertyChanged, IDisposable
    {
        protected ViewModelBase()
        {

        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler hander = this.PropertyChanged;
            if(hander!=null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                hander(this, e);
            }
        }

        public void Dispose()
        {
            this.Dispose();
        }

        protected virtual void OnDispose()
        {

        }
    }

 class MenuListViewModel :ViewModelBase
    {
        readonly MenuRespository _menuRepository;
        public ObservableCollection<Model.Menu> AllMenu
        {
            get;
            private set;
        }

        public MenuListViewModel(MenuRespository menuRepository)
        {
            if(menuRepository==null)
            {
                throw new ArgumentException("menuRepository");
            }
            _menuRepository = menuRepository;
            this.AllMenu = new ObservableCollection<Model.Menu>(_menuRepository.GetMenu());
        }

        protected override void OnDispose()
        {
            //base.OnDispose();
            this.AllMenu.Clear();
        }
    }

MenuListBox.xaml进入View(炫耀“严重性代码描述项目文件行 错误 CS0263 'MenuListBox' 的部分声明不能指定不同的基类

MixNokia.Windows

"

<UserControl
    x:Class="MixNokia.View.MenuListBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MixNokia.View"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel>
        <ListBox ItemsSource="{Binding AllMenu}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding IconThumb}"  Width="40" Height="40" VerticalAlignment="Center" HorizontalAlignment="Left"></Image>
                        <TextBlock Text="{Binding Text}" FontSize="15" Foreground="#CCCCCC" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</UserControl>

决赛

<Page
    x:Class="MixNokia.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MixNokia"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="clr-namespace:MixNokia.ViewModel"
    xmlns:v="clr-namespace:MixNokia.View"
    mc:Ignorable="d">
    <Page.Resources>

        <DataTemplateSelector x:Key="vm:MainWindowViewModel"></DataTemplateSelector>
    </Page.Resources>

<ListBox x:Name="lstMainMenu"
 SelectionChanged="lstMainMenu_SelectionChanged"
 Background="Transparent"
 ItemsSource="{Binding ViewModel}">
I'm developing on universal.

【问题讨论】:

  • MenuListBox 类应该继承自 UserControl
  • 好的。但不要炫耀结果。
  • 黑屏。没有菜单,什么都没有。
  • 真是一团糟。我们不希望转储您的整个格式错误的代码库。我们想要错误消息的完整详细信息。 Sheepy 的链接就是您的答案。您可能在 xaml 中更改了某些内容,却忘记在代码隐藏文件中进行等效更改。

标签: c# wpf mvvm win-universal-app


【解决方案1】:

我看到的,首先在“决赛”上,我没有看到页面的数据上下文在哪里定义,因为数据上下文应该是:

<Page.DataContext>
  <vm:MainWindowViewModel />
</Page.DataContext/>

或者如果你喜欢

<Page ... DataContext="{Binding StaticResource=ViewModel}"...>
 <Page.Resources>
  <vm:MainWindowViewModel x:Key="ViewModel"/>
 </Page.Resources>
</Page>

那么你定义的ViewModels的MainViewModel里面没有'ViewModel'属性,所以应该是

<ListBox x:Name="lstMainMenu" SelectionChanged="lstMainMenu_SelectionChanged" Background="Transparent"
ItemsSource="{Binding ViewModels}">

DataTemplateSelector 为空,用于其他逻辑。

【讨论】:

  • 里面 app.cs 是真的??var viewModel = new MainWindowModel(); MainPage main = new MainPage(); main.DataContext = viewModel;
  • 列表框越来越“mixnokia.viewmodel.menulistviewmodel”
  • 好吧,这不是最好的地方,但你可以在那里这样做,但你是否在 itemssource 的绑定中将“Viewmodel”更改为“ViewModels”?
  • 你的想法是通过mainpage.cs把var viewmodel放到上面。
  • 但是数据上下文是什么? Listbox.itemsource 或 page.itemsource
猜你喜欢
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-30
  • 1970-01-01
  • 2021-08-05
  • 2015-07-13
相关资源
最近更新 更多