我对现有的MVVMlight导航界面方法不太满意,而且我非常小,我想实现我自己的导航界面,我可以在其中展示操纵导航堆栈并将其与MVVM灯集成的复杂方法。
非常感谢任何有关实现这一目标的指导
更新
我想实现页面卷曲,翻转,旋转等页面之间移动的其他过渡
答案 0 :(得分:5)
这是一个完整的实现示例,可以通过添加一个完全替换MvvmLight中的接口的新接口来解决问题,并且还允许您选择是否使用动画。在此示例中,我们添加了控制导航是否应设置动画的功能:
<强>接口强>
public interface ICustomNavigationService
{
string CurrentPageKey { get; }
void GoBack(bool animate = true);
void NavigateTo(string pageKey, bool animate = true);
void NavigateTo(string pageKey, object parameter, bool animate = true);
}
<强>实施强>
public class NavigationService : ICustomNavigationService
{
private readonly Dictionary<string, Type> _pagesByKey = new Dictionary<string, Type>();
private NavigationPage _navigation;
public NavigationPage Navigation
{
get
{
return _navigation;
}
}
public string CurrentPageKey
{
get
{
lock (_pagesByKey)
{
if (_navigation.CurrentPage == null)
{
return null;
}
var pageType = _navigation.CurrentPage.GetType();
return _pagesByKey.ContainsValue(pageType)
? _pagesByKey.First(p => p.Value == pageType).Key
: null;
}
}
}
public void GoBack(bool animate = true)
{
_navigation.PopAsync(animate);
MessagingCenter.Send<INavigationService>(this, "NAVIGATING");
}
public void NavigateTo(string pageKey, bool animate = true)
{
NavigateTo(pageKey, null, animate);
MessagingCenter.Send<INavigationService>(this, "NAVIGATING");
}
public void NavigateTo(string pageKey, object parameter, bool animate = true)
{
lock (_pagesByKey)
{
if (_pagesByKey.ContainsKey(pageKey))
{
var type = _pagesByKey[pageKey];
ConstructorInfo constructor;
object[] parameters;
if (parameter == null)
{
constructor = type.GetTypeInfo()
.DeclaredConstructors
.FirstOrDefault(c => !c.GetParameters().Any());
parameters = new object[]
{
};
}
else
{
constructor = type.GetTypeInfo()
.DeclaredConstructors
.FirstOrDefault(
c =>
{
var p = c.GetParameters();
return p.Count() == 1
&& p[0].ParameterType == parameter.GetType();
});
parameters = new[]
{
parameter
};
}
if (constructor == null)
{
throw new InvalidOperationException(
"No suitable constructor found for page " + pageKey);
}
var page = constructor.Invoke(parameters) as Page;
_navigation.PushAsync(page, animate);
}
else
{
throw new ArgumentException(
string.Format(
"No such page: {0}. Did you forget to call NavigationService.Configure?",
pageKey),
"pageKey");
}
}
}
public void Configure(string pageKey, Type pageType)
{
lock (_pagesByKey)
{
if (_pagesByKey.ContainsKey(pageKey))
{
_pagesByKey[pageKey] = pageType;
}
else
{
_pagesByKey.Add(pageKey, pageType);
}
}
}
public void Initialize(NavigationPage navigation)
{
_navigation = navigation;
}
}
从这里,您可以添加其他您想要的方法。确保将其注入或直接在您使用MvvmLight之前使用它。