简体   繁体   English

windows存储应用程序页面刷新

[英]windows store application page refresh

I am new to windows store application development and currently i am developing a news application and i want to refresh the page to get news updated. 我是Windows商店应用程序开发的新手,目前我正在开发新闻应用程序,我想刷新页面以获取新闻更新。 i stated developing the default layout which is given to us when starting a project and i am lost with the page dictionary because once the page is created. 我说我开发了一个默认布局,它是在启动一个项目时给我们的,因为一旦页面被创建,我就会丢失页面字典。 It gets save so is there a way to refresh a page!!! 它得到保存,所以有一种刷新页面的方法! LoadState method is called when application runs for the first time when refresh is clicked the view get clear but all the data is saved in the dictionary according to my knowledge is there a easy way to clear data inside the groups and recall the methods so that the new data will get filled in.can some one please guide me with the relevant steps 当应用程序第一次运行时调用LoadState方法,当单击刷新时,视图变得清晰但根据我的知识将所有数据保存在字典中是否有一种简单的方法可以清除组内的数据并调用方法以便新数据将被填写。有人请指导我相关的步骤

  protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
    {
       var sampleDataGroups = SampleDataSource.GetGroups((String)navigationParameter);
       this.DefaultViewModel["Groups"] = sampleDataGroups;
    }

   private void refresh(object sender, RoutedEventArgs e)
    {
        this.DefaultViewModel.Clear();

    }

There is no excellent answer to this question. 这个问题没有很好的答案。 But let me talk you through some concepts. 但是,让我谈谈你的一些概念。 Of course, there is no refresh, and the reason for this is many fold. 当然,没有刷新,其原因很多。 Refresh might be added to the Frame someday, but it's problematic right now. 刷新可能会在某天添加到Frame中,但现在它有问题。

Consider a navigation service 考虑导航服务

A good way to navigate in your app is to offload this work to a NavigationService class. 在应用程序中导航的一种好方法是将此工作卸载到NavigationService类。 This is a simple class that has (something like) custom GotoAppHub() and GotoItemDetail(Item) methods. 这是一个简单的类,具有(类似于)自定义GotoAppHub()GotoItemDetail(Item)方法。

The purpose of these methods is not because navigation is difficult, it is because navigation can be centralized. 这些方法的目的不是因为导航很困难,而是因为导航可以集中。 It can check & create the Frame ; 它可以检查和创建Frame ; rather than leaving this erroneously in App.xaml.cs. 而不是将此错误地留在App.xaml.cs.

If your view model wants to navigate to a page, it simply calls NavigationService.GotoItemDetail(item); 如果您的视图模型想要导航到页面,它只需调用NavigationService.GotoItemDetail(item); to do it, passing the item. 要做到这一点,传递项目。 This is smart for a few reasons, let me talk you through them. 这很聪明,有几个原因,让我通过他们谈谈你。

The first reason this is smart is because you may not want to navigate at all. 这很聪明的第一个原因是因为你可能根本不想导航。 In some cases navigation relies on data being loaded or the user having permissions. 在某些情况下,导航依赖于正在加载的数据或具有权限的用户。 Using the navigation service lets you centralize both the navigation logic and the tests necessary to validate the action. 使用导航服务可以集中导航逻辑和验证操作所需的测试。

The second reason this is smart is it allows you to retain the parameter passed to the navigation request. 这很聪明的第二个原因是它允许您保留传递给导航请求的参数。 Remember that the Frame does NOT serialize custom, complex types. 请记住, Frame不会序列化自定义的复杂类型。 As a result, passing item in this case is a bad practice. 因此,在这种情况下传递item是一种不好的做法。 The navigation service can persist this parameter somewhere so the target view model can come pick it up. 导航服务可以将此参数保留在某个位置,以便目标视图模型可以获取它。 Moreover, it can persist it for your Refresh(). 而且,它可以持久化你的Refresh()。

Consider a static repository class 考虑一个静态存储库类

When a view model is loaded, it should be unnecessary for it to know what previous view model caused it to load. 加载视图模型时,它不必知道以前的视图模型导致它加载的内容。 Instead the view model should know what it is supposed to do. 相反,视图模型应该知道它应该做什么。 And to do its work, it needs data. 为了完成它的工作,它需要数据。 Every view model can use the repository class to ask for the "current" record. 每个视图模型都可以使用存储库类来请求“当前”记录。 That current record will be set by the navigation service when navigation occurs. 当导航发生时,导航服务将设置当前记录。 In addition, the repository class knows what to do when there is no current record, fetching it should it need. 此外,当没有当前记录时,存储库类知道该做什么,并在需要时获取它。

In addition to holding a reference to the current record, the repository class also understands persistence. 除了保存对当前记录的引用之外,存储库类还了解持久性。 What I mean is, when the app is loaded, it knows how to fill the lists. 我的意思是,当应用程序加载时,它知道如何填充列表。 When the app is suspended, it knows how to save the data to a file or web service, or whatever you use. 当应用程序暂停时,它知道如何将数据保存到文件或Web服务,或者您使用的任何内容。 The view model, as a result, does not know this and is, as a result, simpler and easier to maintain. 因此,视图模型不知道这一点,因此更简单,更易于维护。

Consider the Reload() method 考虑一下Reload()方法

It is in the navigation service where you need the reload method. 它位于导航服务中,您需要重新加载方法。 Even more than that, it is only in the navigation service where the reload method can be most effective since the navigation service knows the current type and the current parameter value. 更重要的是,只有在导航服务中,重载方法才能最有效,因为导航服务知道当前type和当前参数值。 The navigation service can store these values in local fields and the reload method can simple repeat the navigation. 导航服务可以将这些值存储在本地字段中,重载方法可以简单地重复导航。

Remember the back stack, however. 但请记住后栈。 This is like a browser's navigation and repeating the navigation will mean that the same page will exist twice and the GoBack() method you also have in your navigation service will not go back until you go back twice. 这就像浏览器的导航一样,重复导航将意味着同一页面将存在两次,导航服务中的GoBack()方法将不再返回,直到您返回两次。 The solution is simple, just remember to remove with Services.NavigationService.Instance.Frame.BackStack.Remove() . 解决方案很简单,只需记住使用Services.NavigationService.Instance.Frame.BackStack.Remove()删除。

Remember the cost of loading 记住加载的成本

Sometimes when a page loads there is a considerable cost in loading the UI. 有时,当页面加载时,加载UI会产生相当大的成本。 This is because whatever you are doing isn't trivial. 这是因为无论你做什么都不是微不足道的。 Calling the Reload() will cause the load of the page to repeat. 调用Reload()将导致页面的加载重复。 This is a cost you simply cannot overcome. 这是您无法克服的成本。 But, there might be dependencies on the loading of the page that should be bypassed. 但是,可能存在依赖于应该绕过的页面加载。 For example, you might initiate a web service operation when the page is loaded - and that operation should not be repeated. 例如,您可以在加载页面时启动Web服务操作 - 并且不应重复该操作。 This is up to you to retain a static Boolean that indicates the page has already loaded. 这取决于您保留一个静态布尔值,表示页面已加载。 But it's important you do not forget it. 但重要的是你不要忘记它。

One more benefit of the repository 存储库的另一个好处

A few paragraphs above I mentioned that your navigation service is the one that can remember the last passed parameter for the reload method to work. 上面几段我提到你的导航服务是能够记住上次传递的重载方法工作的参数。 If you have an internal rule that only the navigation service can write to the concurrency class then your navigation service doesn't really have to remember. 如果您有一个内部规则,只有导航服务可以写入并发类,那么您的导航服务实际上不必记住。 It just has to remember the last navigated-to type. 它只需要记住最后一个导航类型。 That's because the concurrency class will already have the reference to the item passed in. This is, however, not always useful. 这是因为并发类已经具有对传入的项的引用。但是,这并不总是有用的。 Sometimes reload is called to throw away the current changes, which means the current item needs to be reloaded or current changes must be flushed. 有时会调用reload来丢弃当前的更改,这意味着需要重新加载当前项目或者必须刷新当前更改。 This will all have to be custom, but I would feel bad if I didn't at least mention it. 这一切都必须是定制的,但如果我至少没有提到它,我会感到很难过。

Conclusion 结论

This should only be taken as a recommendation. 这应该仅作为建议。 Having said that, I have described here the bulk of large, successful WPF and Windows apps in the marketplace. 话虽如此,我在这里描述了市场上大量成功的大型WPF和Windows应用程序。 Using the MVVP pattern, the service pattern for navigation, the repository pattern for concurrency - it's all pretty well proven. 使用MVVP模式,导航的服务模式,并发的存储库模式 - 这一切都得到了很好的证明。 But you are the developer. 但你是开发人员。 In the end, you should choose what is best. 最后,你应该选择什么是最好的。

If you don't like any of that, you can do this: 如果你不喜欢这些,你可以这样做:

public bool Reload()
{
    if (!this.Frame.BackStack.Any())
        return false;
    var current = this.Frame.BackStack.First();
    this.Frame.BackStack.Remove(current);
    return this.Frame.Navigate(current.SourcePageType, current.Parameter);
}

Best of luck! 祝你好运!

How about: 怎么样:

private string parameter;

protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
   parameter = (string)navigationParameter;
   reloadData();
}

private void reloadData() 
{
   var sampleDataGroups = SampleDataSource.GetGroups(parameter);
   this.DefaultViewModel["Groups"] = sampleDataGroups;
}

private void refresh(object sender, RoutedEventArgs e)
{
   reloadData() 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM