Skip to content

Commit

Permalink
Merge pull request PrismLibrary#1843 from andreinitescu/PartialViewsI…
Browse files Browse the repository at this point in the history
…mprovements

Partial views changes
  • Loading branch information
dansiegel committed Jul 4, 2019
2 parents b200a01 + fef2e42 commit 5dbd2e2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
8 changes: 3 additions & 5 deletions Source/Xamarin/Prism.Forms/Common/PageUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ public static void InvokeViewAndViewModelAction<T>(object view, Action<T> action
}
}

if(view is Page page)
if (view is Page page && page.GetPartialViews() is List<BindableObject> partials)
{
var partials = (List<BindableObject>)page.GetValue(ViewModelLocator.PartialViewsProperty) ?? new List<BindableObject>();
foreach(var partial in partials)
foreach (var partial in partials)
{
InvokeViewAndViewModelAction(partial, action);
}
Expand All @@ -48,9 +47,8 @@ public static async Task InvokeViewAndViewModelActionAsync<T>(object view, Func<
}
}

if (view is Page page)
if (view is Page page && page.GetPartialViews() is List<BindableObject> partials)
{
var partials = (List<BindableObject>)page.GetValue(ViewModelLocator.PartialViewsProperty) ?? new List<BindableObject>();
foreach (var partial in partials)
{
await InvokeViewAndViewModelActionAsync(partial, action);
Expand Down
22 changes: 12 additions & 10 deletions Source/Xamarin/Prism.Forms/Mvvm/ViewModelLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public static void SetAutowirePartialView(BindableObject bindable, Page page)
bindable.SetValue(AutowirePartialViewProperty, page);
}

internal static List<BindableObject> GetPartialViews(this Page page)
{
return (List<BindableObject>)page.GetValue(PartialViewsProperty);
}

private static void OnAutowireViewModelChanged(BindableObject bindable, object oldValue, object newValue)
{
bool? bNewValue = (bool?)newValue;
Expand All @@ -65,28 +70,25 @@ private static void OnAutowirePartialViewChanged(BindableObject bindable, object
if (oldValue == newValue)
return;

if(oldValue is Page oldPage)
if (oldValue is Page oldPage)
{
var oldPartials = (List<BindableObject>)oldPage.GetValue(PartialViewsProperty);
oldPartials?.Clear();
oldPage.SetValue(PartialViewsProperty, null);
List<BindableObject> oldPartials = oldPage.GetPartialViews();
oldPartials.Remove(bindable);
}

List<BindableObject> partialViews = null;
if (newValue != null && newValue is Page page)
if (newValue is Page page)
{
// Add View to Views Collection for Page.
partialViews = page.GetValue(PartialViewsProperty) as List<BindableObject>;

if(partialViews == null)
List<BindableObject> partialViews = page.GetPartialViews();
if (partialViews == null)
{
partialViews = new List<BindableObject>();
page.SetValue(PartialViewsProperty, partialViews);
}

partialViews.Add(bindable);
// Set Autowire Property
if(bindable.GetValue(AutowireViewModelProperty) == null)
if (bindable.GetValue(AutowireViewModelProperty) == null)
{
bindable.SetValue(AutowireViewModelProperty, true);
}
Expand Down

0 comments on commit 5dbd2e2

Please sign in to comment.