保持信息在不同页面之间传递

huangapple go评论75阅读模式
英文:

Keep information through different pages

问题

我正在使用WPF C#在不同页面之间进行导航,想知道如何在一页和另一页之间传递信息,但我也在使用Material Design来实现这一点。有什么想法?

我正在使用的代码可以在以下链接找到:
MaterialDesignInXamlToolkit/MainDemo.Wpf/TransitionsDemo/

英文:

im using wpf c# to navigate between various pages and i was wondering how information can be used from 1 page to another, but im using also material design to accomplish this. Any ideas?

The code im using can be found on:
MaterialDesignInXamlToolkit/MainDemo.Wpf/TransitionsDemo/

答案1

得分: 1

你考虑创建SharedDataService类吗?您可以创建服务的单例对象,具有所需的属性,并在需要使用此服务的地方注入它。

public class SharedDataService
{
    string SharedProperty { get; set; }
}

现在在您的类中,您可以像这样使用它:

public class MyClass
{
    private SharedDataService _mySharedService;
    public MyClass(SharedDataService mySharedService)
    {
        _mySharedService = mySharedService;
    }

    public void UpdateSharedProperty()
    {
         SharedProperty = "MyClass中的新值";
    }
}

通过这种方式,您可以从所有需要的类访问SharedDataService。
如果您不喜欢依赖注入,您可以创建静态SharedDataService类。

英文:

Do you think about creating SharedDataService class? You can create singleton object of the service with needed properties and inject it where you need to use same instance of this service.

public class SharedDataService
{
    string SharedProperty {get; set;}
}

and now in you class you can use it like this

public class MyClass
{
    private SharedDataService _mySharedService;
    public MyClass(SharedDataService mySharedService)
    {
        _mySharedService = mySharedService;
    }

    public void UpdateSharedProperty()
    {
         SharedProperty = "New value from MyClass";
    }
}

This way you can access SharedDataService from all needed classes.
If you don't like dependecy injection you can create static SharedDataService class.

huangapple
  • 本文由 发表于 2023年6月8日 17:46:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定