如何创建访问依赖项服务并将参数传递给它调用的方法的方法?



我有一个调用依赖服务的方法。

DependencyService.Get<IPopUp>().Popup("XYZ", "ABC",
(Color)Application.Current.Resources["PopUpTitleColor"],
(Color)Application.Current.Resources["PopUpMessageColor"],
(Color)Application.Current.Resources["PopUpBackgroundColor"],
(Color)Application.Current.Resources["PopUpSeparatorColor"],
(sen, args) => {
DidShowFirstMessage = true;
});

接口:

public interface IPopUp
{
void Popup(string title,
string message,
Color titleColor,
Color messageColor,
Color popUpBackgroundColor,
Color popUpSeparatorColor,
EventHandler handler);
}

有没有办法编写一个帮助程序,该助手将调用相同的依赖项服务并添加参数。

请注意,由于依赖项服务调用多个版本的 Popup,我需要对帮助程序进行编码,以便参数是硬代码。 不作为弹出窗口的扩展方法。这不是我之前问的问题的重复。我只是之前没有完全解释过。

Helper.Popup(("XYZ", "ABC", (sen, args) => {
DidShowFirstMessage = true;
});

这是你需要的吗?我不确定我是否理解你在这里的问题...

static class Helper
{
public static void Popup (string xyz ,string abc ,EventHandler eh)
{
DependencyService.Get<IPopUp>().Popup(xyz, abc,
(Color)Application.Current.Resources["PopUpTitleColor"],
(Color)Application.Current.Resources["PopUpMessageColor"],
(Color)Application.Current.Resources["PopUpBackgroundColor"],
(Color)Application.Current.Resources["PopUpSeparatorColor"],
eh;
}
}

用法

Helper.Popup("XYZ" , "ABC" , (sen, args) => DidShowFirstMessage = true );

但是在这里有一个扩展方法可能是一个更好的选择:

static class Helper
{
public static void Popup (this IDependencyService ds, string xyz ,string abc ,EventHandler eh)
{
ds.Get<IPopUp>().Popup(xyz, abc,
(Color)Application.Current.Resources["PopUpTitleColor"],
(Color)Application.Current.Resources["PopUpMessageColor"],
(Color)Application.Current.Resources["PopUpBackgroundColor"],
(Color)Application.Current.Resources["PopUpSeparatorColor"],
eh;
}
}

用法

DependencyService.Popup("XYZ" , "ABC" , (sen, args) => DidShowFirstMessage = true );

最新更新