- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 mvvm 中遇到中介模式问题
我将描述几乎所有类,以便更好地理解我的问题。
<Window x:Class="TESTPROJECT.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TESTPROJECT"
mc:Ignorable="d"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="MainWindow" Height="500" Width="750">
<Grid>
<ContentControl Grid.Row="1" Content="{Binding PagesControl}"/>
</Grid>
namespace TESTPROJECT
{
class MainWindowViewModel : ViewModelBase
{
private UserControl _pagesControl;
public UserControl PagesControl
{
//Property that switches UserControls
set
{
_pagesControl = value;
OnPropertyChanged();
}
get
{
return _pagesControl;
}
}
public MainWindowViewModel()
{
//Method that will be listening all the changes from UserControls ViewModels
Mediator.Instance.Register(
(object obj) =>
{
PagesControl = obj as UserControl;
}, ViewModelMessages.UserWroteSomething);
}
}
}
<UserControl x:Class="TESTPROJECT.FirstUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TESTPROJECT"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Command="{Binding GetCommand}">
hello, i'm first user control!
</Button>
</Grid>
namespace TESTPROJECT
{
class FirstUserControlViewModel : ViewModelBase
{
//command that is binded to button
private DelegateCommand getCommand;
public ICommand GetCommand
{
get
{
if (getCommand == null)
getCommand = new DelegateCommand(param => this.func(param), null);
return getCommand;
}
}
//method that will handle button click, and in it i'm sending a message
//to MainWindowViewModel throug Mediator class
//and that is allso a problem place because in theory i should
//pass the opposite UserControl object , but from here i have no
//acces to it
private void func(object obj)
{
Mediator.Instance.NotifyColleagues(
ViewModelMessages.UserWroteSomething,
"PROBLEM PLACE");
}
}
<UserControl x:Class="TESTPROJECT.SecondUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TESTPROJECT"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Command="{Binding GetCommand}">
Hello, i'm second user control!
</Button>
</Grid>
namespace TESTPROJECT
{
class SecondUserControlViewModel : ViewModelBase
{
//command that is binded to button
private DelegateCommand getCommand;
public ICommand GetCommand
{
get
{
if (getCommand == null)
getCommand = new DelegateCommand(param => this.func(param), null);
return getCommand;
}
}
//method that will handle button click, and in it i'm sending a message
//to MainWindowViewModel throug Mediator class
//and that is allso a problem place because in theory i should
//pass the opposite UserControl object , but from here i have no
//acces to it
private void func(object obj)
{
Mediator.Instance.NotifyColleagues(
ViewModelMessages.UserWroteSomething,
"PROBLEM PLACE");
}
}
namespace TESTPROJECT
{
//this enum holding some kind of event names fro example UserWroteSomething
// is a name of switching one UserControl to another
public enum ViewModelMessages { UserWroteSomething = 1 };
class Mediator
{
//Singletone part
private static Mediator instance;
public static Mediator Instance
{
get
{
if (instance == null)
instance = new Mediator();
return instance;
}
}
private Mediator() { }
//Singletone part
//collection listeners that holds event names and handler functions
List<KeyValuePair<ViewModelMessages, Action<Object>>> internalList =
new List<KeyValuePair<ViewModelMessages, Action<Object>>>();
//new listener registration
public void Register(Action<object> callBack, ViewModelMessages message)
{
internalList.Add(
new KeyValuePair<ViewModelMessages, Action<Object>>(message, callBack));
}
// notifying all the listener about some changes
// and those whose names fits will react
public void NotifyColleagues(ViewModelMessages message, object args)
{
foreach(KeyValuePair<ViewModelMessages, Action<Object>> KwP in internalList)
if(KwP.Key == message)
KwP.Value(args);
}
}
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
FirstUserControl first = new FirstUserControl() { DataContext = new FirstUserControlViewModel() };
SecondUserControl second = new SecondUserControl() { DataContext = new SecondUserControlViewModel() };
new MainWindow()
{
DataContext = new MainWindowViewModel() { PagesControl = first }
}.ShowDialog();
}
}
最佳答案
如果我对您的理解正确,当当前事件 View 模型上发生特定操作(例如,您按下按钮)时,您希望导航到另一个 View (或分别为 View 模型)。
如果你想为此使用你的中介,你可以像这样构造它:
public class Mediator
{
// These fields should be set via Constructor Injection
private readonly MainWindowViewModel mainWindowViewModel;
private readonly Dictionary<ViewModelId, IViewFactory> viewFactories;
public void NotifyColleagues(ViewModelId targetViewModelId, ViewModelArguments arguments)
{
var targetFactory = this.viewModelFactories[targetViewModelId];
var view = targetFactory.Create(viewModelArguments);
this.mainWindowViewModel.PagesControl = view;
}
// other members omitted to keep the example small
}
ViewModelArguments
,您可以将来自其他 View 模型的信息传递到新创建的 View 模型中。 ViewModelId 可以像您的 ViewModelMessage 一样是一个简单的枚举,您也可以使用
Type
的 View 模型(我会建议你去追求)。
Mediator
上使用私有(private)构造函数。类,因为否则您无法传入 mainWindowViewModel 和 View 工厂的字典。您应该能够在您的应用程序启动方法中配置它。
关于c# - 无法在 MVVM 中完成模式中介,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37103296/
我试图了解 ESB Mediation是,确切地说(具体例子),并且正在撞墙。 根据维基百科,数据中介重定向到 Data Transformation文章,唯一提到调解是模糊和神秘的: When th
我似乎有 admob 调解工作正常。我宁愿先使用 iAd,然后再使用 admob 填充。问题是我在 iTunes 连接中收到的 iAd 请求为零。我什至将百分比设置为 100%。我启用了它,一切都在
我有一个带有 ID、NAME、DATE 的 MySql 数据库我想通过使用 dblookup 介体来获取这些行,似乎不起作用,有人可以检查我的代理定义吗?
我正在尝试设置我的 Android 应用程序以显示来自中介网络的广告(顺便说一句,在任何地方都没有很好的解释),这是我到目前为止所做的: 1) 我已将发布商 ID 复制到将显示广告的 xml View
我的 AdMob 中介正在运行,它可以很好地提供 AdMob 广告,但是当我输入 MMedia 或 MobFox 的有效 ID 时,我收到请求,但填充数为 0。有谁知道这是为什么吗? 最佳答案 您还需
.h #import #import #import "GADBannerView.h" @interface MasterTableViewController : UITableViewCon
开门见山: 我了解什么是 mediationID 及其用途,但我无法在 admob 上获取 mediationID工具。 Mediation ad network guide看起来像这样: 但现实是我
我无法在 Android 应用程序中调解 admob 和 Facebook 受众网络 (FAN)。该应用仅转换来自 Admob 网络的广告。 Admob 和 FAN 之间的中介不起作用。以下是我遵循的
我目前在我的应用程序中使用 AdMob,我想集成 StartApp 但我想通过 AdMob 中介来管理它,有什么方法可以做到这一点(到目前为止我找不到任何文档,他们的支持也没有回答这个问题)? 最佳答
我正在尝试使用 InMobi 实现 AdMob 中介,但广告不会显示。我关注 their documentation我已经添加了 SDK 和 AndroidX: implementation 'com
有人知道如何使用 adMob 中介来管理 MMedia 的插页式广告吗? 我实际上使用 1) adMob Floor 2)MMedia(ID 横幅广告)3)admob“标准”。 想法? 在此先感谢您的
我想知道 Admob 中介的正确范例是什么。 到目前为止,我得到了一个普通的 AdView 横幅。 这就是它应该如何工作: onFailedToReceiveAd - 捕获此事件,这意味着 admob
背景 我们有一个相当复杂的 Silverlight 客户端,我们正在用 HTML/Javascript/CSS 重写它,构建在相同的 Web 服务之上。实际上,我们正在移植两个 Silverlight
是否有程序化方式来判断 AdMob 中介是展示 iAd 还是 AdMob 广告? 我的 GADBannerView 的父 View 必须根据正在展示的广告做一些特别的事情...... 最佳答案 您可以
我在使用 Applovin 和 Chartboost 实现 AdMob 奖励视频中介时被困了 3 天,代码完全找到了,但是当我启动应用程序时,它说“onRewardedVideoAdFailedToL
为此,我正在尝试将 iAds 集成到 Admob 中介 SDK 中,我从 Mediation website Download Link 下载了 iAdListAdapter并将其添加到我的 xcod
我是跟着Google的Admob/iAd mediation一步一步来的。我只会得到一个错误。这是确切的错误。我从 Xcode 复制并粘贴。 Ld /Users/RobShi/Desktop/Robf
我正在与多个第三方网络实现 AdMob 中介。我想测试这些网络中的每一个的调解是否得到很好的实现(广告很好地展示)。 我想强制显示来自定义网络的广告以测试其实现,然后快速切换到另一个网络。 执行此操作
我正在尝试通过 MoPub 调解 Facebook Audience Network,但我没有任何原生广告设置。我只想调解横幅广告和插页式广告。在 MoPub 文档中,没有关于横幅广告和插页式广告的说
假设我有一个 std::set , 我想知道它是否包含字符串 "name": #include #include using namespace std; bool has_name(const
我是一名优秀的程序员,十分优秀!