本文整理汇总了C#中ViewModelBase.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# ViewModelBase.GetType方法的具体用法?C# ViewModelBase.GetType怎么用?C# ViewModelBase.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ViewModelBase
的用法示例。
在下文中一共展示了ViewModelBase.GetType方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateWindow
Window CreateWindow(ViewModelBase viewModel)
{
Type windowType;
lock (_viewMap)
{
if (!_viewMap.ContainsKey(viewModel.GetType()))
throw new ArgumentException("viewModel not registered");
windowType = _viewMap[viewModel.GetType()];
}
var window = (Window)Activator.CreateInstance(windowType);
window.DataContext = viewModel;
window.Closed += OnClosed;
lock (_openedWindows)
{
// Last window opened is considered the 'owner' of the window.
// May not be 100% correct in some situations but it is more
// then good enough for handling dialog windows
if (_openedWindows.Count > 0)
{
Window lastOpened = _openedWindows[_openedWindows.Count - 1];
if (window != lastOpened)
window.Owner = lastOpened;
}
_openedWindows.Add(window);
}
// Listen for the close event
Messenger.Default.Register<RequestCloseMessage>(window, viewModel, OnRequestClose);
return window;
}
示例2: ProvideView
public static void ProvideView(ViewModelBase vm, ViewModelBase parentVm = null)
{
IViewMap map = Maps.SingleOrDefault(x => x.ViewModelType == vm.GetType());
if (map == null)
return;
var viewInstance = Activator.CreateInstance(map.ViewType) as Window;
if (viewInstance == null)
throw new InvalidOperationException(string.Format("Can not create an instance of {0}", map.ViewType));
if (parentVm != null)
{
ViewInstance parent = RegisteredViews.SingleOrDefault(x => x.ViewModel == parentVm);
if (parent != null)
{
Window parentView = parent.View;
if (Application.Current.Windows.OfType<Window>().Any(x => Equals(x, parentView)))
viewInstance.Owner = parentView;
}
}
viewInstance.DataContext = vm;
RegisteredViews.Add(new ViewInstance(viewInstance, vm));
viewInstance.Show();
}
示例3: DelegatePropertyDescriptor
public DelegatePropertyDescriptor(string descriptor_name, ViewModelBase view_model_base, Type property_type)
: base(descriptor_name, null)
{
this.owner_type = view_model_base.GetType();
this.property_type = property_type;
this.descriptor_name = descriptor_name;
this.view_model_base = view_model_base;
}
示例4: GetTemplate
public static DataTemplate GetTemplate(ViewModelBase param)
{
Type t = param.GetType();
if(!views.ContainsKey(t.Name))
{
views.Add(t.Name, App.Current.Resources[t.Name] as DataTemplate);
}
return views[t.Name];
}
示例5: GetWindow
/// <summary>
/// Gets a <see cref="Window"/> to display the associated content for a <see cref="ViewModelBase"/>
/// </summary>
/// <param name="viewModel"></param>
/// <returns></returns>
private static Window GetWindow(ViewModelBase viewModel)
{
if (viewModel == null)
throw new ArgumentNullException("viewModel");
// get the view template
var viewTemplate = Application.Current.TryFindResource(new DataTemplateKey(viewModel.GetType())) as DataTemplate;
// can't do anything if no view type found for view model
if (viewTemplate == null) return null;
// get content from data template
var view = viewTemplate.LoadContent();
// if view is Window, just show it - otherwise, create window and wrap content in it
return view as Window ?? new Window { SizeToContent = SizeToContent.WidthAndHeight, Content = view };
}
示例6: TryInjectQueryString
private void TryInjectQueryString(ViewModelBase viewModel, PhoneApplicationPage page)
{
var viewModelType = viewModel.GetType();
foreach (var pair in page.NavigationContext.QueryString)
{
var property = viewModelType.GetPropertyCaseInsensitive(pair.Key);
if (property == null)
continue;
//property.SetValue(viewModel,MessageBinder.CoerceValue(property.PropertyType, pair.Value, page.NavigationContext),
// null
// );
if (property.PropertyType == typeof(int))
{
property.SetValue(viewModel, int.Parse(pair.Value), null);
}
else
{
property.SetValue(viewModel, pair.Value, null);
}
}
}
示例7: GenerateXamlFromClass
public static string GenerateXamlFromClass(ViewModelBase BindTarget)
{
var template1 = "<TextBlock x:Name=\"{0}Label\" FontSize=\"28\" Text=\"{{x:Bind {1}.{0}Label}}\"></TextBlock>";
var template4 = "<TextBlock x:Name=\"{0}Label\" FontSize=\"28\" Text=\"{{x:Bind {1}.{0}Label}}\" RelativePanel.Below=\"{2}Label\"></TextBlock>";
var template2 = "<TextBlock x:Name=\"{0}\" Text=\"{{x:Bind {1}.{0}, Mode=OneWay}}\" FontSize=\"28\" RelativePanel.RightOf=\"{0}Label\" RelativePanel.AlignVerticalCenterWith=\"{0}Label\"></TextBlock>";
var template3 = "<TextBlock x:Name=\"{0}\" Text=\"{{x:Bind {1}.{0}, Mode=OneWay}}\" FontSize=\"28\" RelativePanel.RightOf=\"{0}Label\" RelativePanel.Below=\"{2}\" RelativePanel.AlignVerticalCenterWith=\"{0}Label\"></TextBlock>";
var t = BindTarget.GetType();
var props = t.GetRuntimeProperties();
var sb = new StringBuilder();
string prev = null;
foreach (var prop in props)
{
if (prop.Name.EndsWith("Label"))
continue;
if (string.IsNullOrEmpty(prev))
{
sb.Append(string.Format(template1, prop.Name, t.Name));
sb.AppendLine();
sb.Append(string.Format(template2, prop.Name, t.Name));
}
else
{
sb.Append(string.Format(template4, prop.Name, t.Name, prev));
sb.AppendLine();
sb.Append(string.Format(template3, prop.Name, t.Name, prev));
}
sb.AppendLine();
prev = prop.Name;
}
return sb.ToString();
}
示例8: GetTemplate
public static DataTemplate GetTemplate(ViewModelBase param)
{
Type t = param.GetType();
return App.Current.Resources[t.Name] as DataTemplate;
}
示例9: GetMultilingualPropertiesFromViewModel
private IEnumerable<PropertyInfo> GetMultilingualPropertiesFromViewModel(ViewModelBase viewModel)
{
return viewModel.GetType().GetProperties().Where(pi => pi.CustomAttributes.Any(ca => ca.AttributeType == typeof(MultilingualAttribute)));
}
示例10: Dump
public static string Dump(ViewModelBase viewModel)
{
try
{
if (viewModel is LinkRiverViewModel)
{
var linkRiver = viewModel as LinkRiverViewModel;
string selectedId = null;
if (linkRiver.CurrentSelected != null)
selectedId = linkRiver.CurrentSelected.Id;
var serializationTpl = new Tuple<Subreddit, string, List<Link>, DateTime, string, string>(linkRiver.Thing, linkRiver.Sort,
linkRiver.Links
.Take(100)
.Select(lvm => ((LinkViewModel)lvm).Link)
.ToList(),
linkRiver.LastRefresh ?? DateTime.Now, selectedId, linkRiver.Category);
var serialized = JsonConvert.SerializeObject(serializationTpl);
return JsonConvert.SerializeObject(Tuple.Create("LinkRiverViewModel", serialized));
}
else if (viewModel is CommentsViewModel)
{
var comments = viewModel as CommentsViewModel;
return JsonConvert.SerializeObject(Tuple.Create("CommentsViewModel", JsonConvert.SerializeObject(Tuple.Create(comments.DumpListing(), comments.Link.Url, comments.Link.Link.Id, comments.LastRefresh))));
}
else if (viewModel is LockScreenViewModel
|| viewModel is SettingsViewModel)
{
return JsonConvert.SerializeObject(Tuple.Create("SettingsViewModel", ""));
}
else if (viewModel is PostViewModel)
{
var postViewModel = viewModel as PostViewModel;
return JsonConvert.SerializeObject(Tuple.Create("PostViewModel", JsonConvert.SerializeObject(new { Editing = postViewModel.Editing, Kind = postViewModel.Kind, Subreddit = postViewModel.Subreddit, Text = postViewModel.Text, Title = postViewModel.Title, Url = postViewModel.Url })));
}
else if (viewModel is ConversationViewModel)
{
var conversationViewModel = viewModel as ConversationViewModel;
return JsonConvert.SerializeObject(Tuple.Create("ConversationViewModel",
conversationViewModel.IsEditing ? JsonConvert.SerializeObject(new
{
ActivityId = conversationViewModel.CurrentGroup.Id,
Username = conversationViewModel.Reply.Username,
Topic = conversationViewModel.Reply.Topic,
Contents = conversationViewModel.Reply.Contents,
IsReply = conversationViewModel.Reply.IsReply
}) :
JsonConvert.SerializeObject(new
{
ActivityId = conversationViewModel.CurrentGroup.Id
})));
}
else if (viewModel is CommentsContentStreamViewModel)
{
var contentStream = viewModel as CommentsContentStreamViewModel;
var currentSelectedUrl = contentStream.CurrentSelected.Url;
var currentSelectedId = contentStream.CurrentSelected.Id;
return JsonConvert.SerializeObject(Tuple.Create("CommentsContentStreamViewModel", JsonConvert.SerializeObject(Tuple.Create(currentSelectedId, currentSelectedUrl))));
}
else if (viewModel is LoginViewModel)
{
return JsonConvert.SerializeObject(Tuple.Create("LoginViewModel", ""));
}
else if (viewModel is AboutUserViewModel)
{
return JsonConvert.SerializeObject(Tuple.Create("AboutUserViewModel", JsonConvert.SerializeObject(Tuple.Create(((AboutUserViewModel)viewModel).Thing, ((AboutUserViewModel)viewModel).LastRefresh ?? DateTime.UtcNow))));
}
else
throw new InvalidOperationException(viewModel.GetType().FullName);
}
catch (Exception ex)
{
//_logger.Fatal(string.Format("type was {0}", viewModel != null ? viewModel.GetType().FullName : "null"), ex);
throw;
}
}
示例11: UpdateNavigationStack
private void UpdateNavigationStack(ViewModelBase newViewModel)
{
if (newViewModel.IsBackNavigationEnabled)
{
var newViewModelType = newViewModel.GetType();
// Hvis den vm der hoppes hen på findes i stacken i forvejen, går man tilbage.
// Ellers går man frem på en ny vm, som skal tilføjes til stacken.
if (_navigationStack.Contains(newViewModelType))
{
// Fjern fra stacken indtil den der hoppes tilbage til er fjernet.
// Hvis man kun går et step tilbage fjernes kun et element, men man kan også navigere flere steps tilbage,
// hvorved de views der springes over også skal fjernes fra stacken.
Type poppedViewModelType = null;
while (poppedViewModelType != newViewModelType)
poppedViewModelType = _navigationStack.Pop();
}
else
{
if (_currentViewModel != null)
_navigationStack.Push(_currentViewModel.GetType());
}
}
else
{
_navigationStack.Clear();
}
}
示例12: GetName
public string GetName(ViewModelBase model)
{
return GetName(model.GetType());
}