C#Windows 10 Universal app - MVVM刷新

时间:2015-10-07 13:11:28

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

我在使用MVVM的Windows 10 Universal App上开发了一个应用程序,但我遇到了很大问题 我将一个ObservableCollection项(在第二个窗口上创建)添加到MVVM然后,在MainPage的ListView上显示新项,但它不刷新!

2个窗口始终打开

http://i.stack.imgur.com/WSo6v.jpg

MVVMList.cs的代码

public class MVVMList : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private ObservableCollection<THEFile> onglets_cache = new ObservableCollection<THEFile>();
    public ObservableCollection<THEFile> onglets_list
    {
        get
        {
            return onglets_cache;
        }

        set
        {
            onglets_cache = value;
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this,
                    new PropertyChangedEventArgs("onglets_list"));
        }
    }

    public MVVMList()
    {
        onglets_list = new ObservableCollection<THEFile>();
        Fonctions fonctions = new Fonctions();
        fonctions.LoadOnglets(onglets_cache);

    }

}

第二页的代码(始终打开) - CreateFile.xaml.cs

    private void create_butt_Click(object sender, RoutedEventArgs e)
    {
        Fonctions fonc = new Fonctions(); MVVMList main = new MVVMList();
        fonc.SetupNew(main.onglets_list, "test" + ".php", "");
    }

    //SetupNew on Fonctions.cs
    public async void SetupNew(ObservableCollection<THEFile> list, string name, string content)
    {
        FolderPicker folderpick = new FolderPicker();
        folderpick.ViewMode = PickerViewMode.List;
        folderpick.FileTypeFilter.Add(".html"); folderpick.FileTypeFilter.Add(".htm"); folderpick.FileTypeFilter.Add(".HTML");
        folderpick.FileTypeFilter.Add(".php"); folderpick.FileTypeFilter.Add(".PHP");
        folderpick.FileTypeFilter.Add(".css"); folderpick.FileTypeFilter.Add(".CSS");
        folderpick.FileTypeFilter.Add(".js"); folderpick.FileTypeFilter.Add(".JS");

        StorageFolder storage_file = await folderpick.PickSingleFolderAsync();
        if (storage_file != null)
        {
            MainPage vm = new MainPage();
            list.Add(new THEFile { NameOfFile = name, PathOfFile = storage_file.Path + "\\" + name, CodeOfFile = content, already_opened = false, line = 0 });
            string path = storage_file.Path + @"\" + name;
            StorageFile file_create = await storage_file.CreateFileAsync(name, CreationCollisionOption.GenerateUniqueName);
            Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file_create);
            SaveOnglets(list);
        }
    }

在MainPage.xaml上(始终打开)

...
<ListView x:Name="onglets" x:FieldModifier="public" ItemTemplate="{StaticResource Templa}" ItemsSource="{Binding onglets_list}" SelectionChanged="onglets_SelectionChanged" Margin="0,117,0,57" Visibility="Visible" ContainerContentChanging="onglets_ContainerContentChanging">
...

谢谢!

2 个答案:

答案 0 :(得分:0)

在您的XAML中,尝试使用Collection View Source。

将其添加到xaml的顶部:

<Page.Resources>
    <CollectionViewSource x:Name="MakesCollectionViewSource" IsSourceGrouped="True"/>
</Page.Resources>

设置ListView:

ItemsSource="{Binding Source={StaticResource MakesCollectionViewSource}}"

然后在您的代码中,当您有一个项目列表时,使用

分配它
MakesCollectionViewSource.Source = /* Some List<GroupInfoList<object>> that is generated from onglets_list*/

我像这样创建我的列表,但它可能不相关,因为这是为了使我的所有对象名称都按字母顺序排列:

    internal List<GroupInfoList<object>> GetGroupsByLetter()
    {
        var groups = new List<GroupInfoList<object>>();

        var query = from item in MakeList
                    orderby ((Make)item).MakeName
                    group item by ((Make)item).MakeName[0] into g
                    select new { GroupName = g.Key, Items = g };
        foreach (var g in query)
        {
            var info = new GroupInfoList<object>();
            info.Key = g.GroupName;
            foreach (var item in g.Items)
            {
                info.Add(item);
            }

            groups.Add(info);
        }

        return groups;
    }

public class GroupInfoList<T> : List<object>
{
    public object Key { get; set; }

    public new IEnumerator<object> GetEnumerator()
    {
        return (System.Collections.Generic.IEnumerator<object>)base.GetEnumerator();
    }
}

其中MakeList是我的可观察集合,Make是集合中的对象,MakeName是我想要按字母顺序排列的字符串

然后使用

进行呼叫
MakesCollectionViewSource.Source = GetGroupsByLetter();

答案 1 :(得分:0)

如果我正确理解您的代码和要求,我认为问题的一部分是您&#34; new up&#34;每次单击“创建”按钮时,都会显示MVVMList和MainPage。

因此,如果不了解有关使用MVVM Light和IOC容器的建议,您可以通过使MVVMList类成为单例并让MainPage将其用于数据上下文来快速完成您尝试执行的操作。当您的其他窗口添加到MVVMList.onglets集合时,它将立即反映在您当前打开的MainPage中。如果您需要一些代码段,请告诉我。祝你好运!

[编辑如下]

午餐时间还剩下几分钟,所以这里有一个过于简化的例子。再次,没有进入MVVM是什么,不是。就个人而言,我会以不同的方式做到这一点,但这不在你问题的范围之内。完全披露 - 这是在WPF中,但同样的逻辑适用,我只是没有在我正在使用的PC上安装Windows 10。我还将集合简化为字符串类型。这不是为了复制/粘贴到您的代码中,因为它在您的示例中不起作用 - 但应该很容易转移。

MVVMList类:

    public class MVVMList: INotifyPropertyChanged
        {
    //Singleton section
            private static MVVMList instance;
            private MVVMList() { }
            public static MVVMList Instance
            {
                get
                {
                    if (instance == null)
                    {
                        instance = new MVVMList();
                    }
                    return instance;
                }
            }
 //end singleton section           
            private ObservableCollection<string> _onglets = new ObservableCollection<string>();
            public ObservableCollection<string> Onglets
            {
                get { return _onglets; }
                set
                {
                    if (_onglets != value)
                    {
                        _onglets = value;
                        if (PropertyChanged != null)
                            PropertyChanged.Invoke(this,
                                new PropertyChangedEventArgs("onglets_list"));
                    }
                }
            }

    //INotify implementation 
            public event PropertyChangedEventHandler PropertyChanged;
        }

的MainPage:

<ListView x:Name="onglets" x:FieldModifier="public" ItemsSource="{Binding Onglets}" />

MainPage.cs:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = MVVMList.Instance;
            Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var x = new CreateWindow();
            x.Show();
        }
    }

CreateWindow.cs:

private void CreateButton_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(StringTextBox.Text))
            {
                MVVMList.Instance.Onglets.Add(StringTextBox.Text);
            }
        }