c# Windows 10通用应用程序- MVVM刷新

本文关键字:MVVM 刷新 应用程序 Windows 10通 | 更新日期: 2023-09-27 18:16:22

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

两个窗口总是开着的

https://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 . example .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);
        }
    }

在主页上。Xaml (always open)

...
<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">
...

谢谢!

c# Windows 10通用应用程序- MVVM刷新

在您的XAML中,尝试使用集合视图源。

添加到你的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*/

我像这样创建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();

如果我正确理解您的代码和要求,我认为问题的一部分是您每次单击创建按钮时"新建"您的MVVMList和MainPage。

所以,没有进入关于使用mvvmlight和IOC容器的建议,你可以通过使你的MVVMList类成为单例并让你的MainPage使用它作为数据上下文来快速完成你想要做的事情。当您的其他窗口添加到MVVMList时。集合时,它将立即反映在当前打开的主页中。如果你需要一些代码片段,请告诉我。好运!

[编辑下面]

我还有几分钟吃午饭,所以这里有一个过于简化的例子。再说一遍,我不知道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;
        }

主页:

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