Xamarin Forms State Container View not Respecting Layout Options











up vote
5
down vote

favorite












I am using a basic State Management system found here: (code below)



https://github.com/xDelivered-Patrick/Xamarin.Forms.Essentials/blob/master/Essentials/Controls/State/StateContainer.cs



When implementing, my XAML View looks like the below.



The problem is the StackLayout CenterAndExpand is not working. All the views when rendered are simply scrunched at the top.



If I pull the particular section out of the StateContainer and eliminate its usage, it renders correctly. Is this an issue with the way the View is being rendered inside the Content of the StateContainer?



EDIT: Here is a minimal example: https://github.com/aherrick/StateManagementDemo



XAML:



<ContentPage.Content>

<StackLayout x:Name="layoutWrap" VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">

<controls:StateContainer State="{Binding State}">
<controls:StateCondition Is="Loading">

<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">

<ActivityIndicator x:Name="loadingIndicator"
IsVisible="{Binding IsBusy}"
IsRunning="{Binding IsBusy}"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand" />
</StackLayout>
</controls:StateCondition>

<controls:StateCondition Is="Loaded">
<!-- actual content here -->
</controls:StateCondition>
<controls:StateCondition Is="Error">

<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">

<Label FontAttributes="Bold" Text="Oops! There was a problem." VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />
<Button Text="Tap to Retry" Command="{Binding LoadVenuesCommand}" CommandParameter="true" HorizontalOptions="CenterAndExpand" TextColor="White" Padding="15" BackgroundColor="#26265E" />
</StackLayout>
</controls:StateCondition>
</controls:StateContainer>
</StackLayout>
</ContentPage.Content>


C#



[ContentProperty("Content")]
[Preserve(AllMembers = true)]
public class StateCondition : View
{
public object Is { get; set; }
public object IsNot { get; set; }
public View Content { get; set; }
}

[ContentProperty("Conditions")]
[Preserve(AllMembers = true)]
public class StateContainer : ContentView
{
public List<StateCondition> Conditions { get; set; } = new List<StateCondition>();

// https://forums.xamarin.com/discussion/102024/change-bindableproperty-create-to-bindableproperty-create
//public static readonly BindableProperty StateProperty = BindableProperty.Create(nameof(State), typeof(object), typeof(StateContainer), propertyChanged: StateChanged);

public static readonly BindableProperty StateProperty = BindableProperty.Create<StateContainer, object>(x => x.State, null, propertyChanged: StateChanged);

public static void Init()
{
}

private static void StateChanged(BindableObject bindable, object oldValue, object newValue)
{
var parent = bindable as StateContainer;
parent?.ChooseStateProperty(newValue);
}

public object State
{
get { return GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}

private void ChooseStateProperty(object newValue)
{
foreach (StateCondition stateCondition in Conditions)
{
if (stateCondition.Is != null)
{
if (stateCondition.Is.ToString().Equals(newValue.ToString()))
{
Content = stateCondition.Content;
}
}
else if (stateCondition.IsNot != null)
{
if (!stateCondition.IsNot.ToString().Equals(newValue.ToString()))
{
Content = stateCondition.Content;
}
}
}
}
}









share|improve this question
























  • No thoughts on this? I've included a minimal example of the issue above in my GitHub
    – aherrick
    Nov 17 at 13:58










  • Have you solved it?
    – Alexander Uddfeldt
    Nov 20 at 13:36










  • I ran a quick test on an android device. It seems to work as i described it bellow. Try it!
    – Alexander Uddfeldt
    Nov 20 at 13:51















up vote
5
down vote

favorite












I am using a basic State Management system found here: (code below)



https://github.com/xDelivered-Patrick/Xamarin.Forms.Essentials/blob/master/Essentials/Controls/State/StateContainer.cs



When implementing, my XAML View looks like the below.



The problem is the StackLayout CenterAndExpand is not working. All the views when rendered are simply scrunched at the top.



If I pull the particular section out of the StateContainer and eliminate its usage, it renders correctly. Is this an issue with the way the View is being rendered inside the Content of the StateContainer?



EDIT: Here is a minimal example: https://github.com/aherrick/StateManagementDemo



XAML:



<ContentPage.Content>

<StackLayout x:Name="layoutWrap" VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">

<controls:StateContainer State="{Binding State}">
<controls:StateCondition Is="Loading">

<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">

<ActivityIndicator x:Name="loadingIndicator"
IsVisible="{Binding IsBusy}"
IsRunning="{Binding IsBusy}"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand" />
</StackLayout>
</controls:StateCondition>

<controls:StateCondition Is="Loaded">
<!-- actual content here -->
</controls:StateCondition>
<controls:StateCondition Is="Error">

<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">

<Label FontAttributes="Bold" Text="Oops! There was a problem." VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />
<Button Text="Tap to Retry" Command="{Binding LoadVenuesCommand}" CommandParameter="true" HorizontalOptions="CenterAndExpand" TextColor="White" Padding="15" BackgroundColor="#26265E" />
</StackLayout>
</controls:StateCondition>
</controls:StateContainer>
</StackLayout>
</ContentPage.Content>


C#



[ContentProperty("Content")]
[Preserve(AllMembers = true)]
public class StateCondition : View
{
public object Is { get; set; }
public object IsNot { get; set; }
public View Content { get; set; }
}

[ContentProperty("Conditions")]
[Preserve(AllMembers = true)]
public class StateContainer : ContentView
{
public List<StateCondition> Conditions { get; set; } = new List<StateCondition>();

// https://forums.xamarin.com/discussion/102024/change-bindableproperty-create-to-bindableproperty-create
//public static readonly BindableProperty StateProperty = BindableProperty.Create(nameof(State), typeof(object), typeof(StateContainer), propertyChanged: StateChanged);

public static readonly BindableProperty StateProperty = BindableProperty.Create<StateContainer, object>(x => x.State, null, propertyChanged: StateChanged);

public static void Init()
{
}

private static void StateChanged(BindableObject bindable, object oldValue, object newValue)
{
var parent = bindable as StateContainer;
parent?.ChooseStateProperty(newValue);
}

public object State
{
get { return GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}

private void ChooseStateProperty(object newValue)
{
foreach (StateCondition stateCondition in Conditions)
{
if (stateCondition.Is != null)
{
if (stateCondition.Is.ToString().Equals(newValue.ToString()))
{
Content = stateCondition.Content;
}
}
else if (stateCondition.IsNot != null)
{
if (!stateCondition.IsNot.ToString().Equals(newValue.ToString()))
{
Content = stateCondition.Content;
}
}
}
}
}









share|improve this question
























  • No thoughts on this? I've included a minimal example of the issue above in my GitHub
    – aherrick
    Nov 17 at 13:58










  • Have you solved it?
    – Alexander Uddfeldt
    Nov 20 at 13:36










  • I ran a quick test on an android device. It seems to work as i described it bellow. Try it!
    – Alexander Uddfeldt
    Nov 20 at 13:51













up vote
5
down vote

favorite









up vote
5
down vote

favorite











I am using a basic State Management system found here: (code below)



https://github.com/xDelivered-Patrick/Xamarin.Forms.Essentials/blob/master/Essentials/Controls/State/StateContainer.cs



When implementing, my XAML View looks like the below.



The problem is the StackLayout CenterAndExpand is not working. All the views when rendered are simply scrunched at the top.



If I pull the particular section out of the StateContainer and eliminate its usage, it renders correctly. Is this an issue with the way the View is being rendered inside the Content of the StateContainer?



EDIT: Here is a minimal example: https://github.com/aherrick/StateManagementDemo



XAML:



<ContentPage.Content>

<StackLayout x:Name="layoutWrap" VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">

<controls:StateContainer State="{Binding State}">
<controls:StateCondition Is="Loading">

<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">

<ActivityIndicator x:Name="loadingIndicator"
IsVisible="{Binding IsBusy}"
IsRunning="{Binding IsBusy}"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand" />
</StackLayout>
</controls:StateCondition>

<controls:StateCondition Is="Loaded">
<!-- actual content here -->
</controls:StateCondition>
<controls:StateCondition Is="Error">

<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">

<Label FontAttributes="Bold" Text="Oops! There was a problem." VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />
<Button Text="Tap to Retry" Command="{Binding LoadVenuesCommand}" CommandParameter="true" HorizontalOptions="CenterAndExpand" TextColor="White" Padding="15" BackgroundColor="#26265E" />
</StackLayout>
</controls:StateCondition>
</controls:StateContainer>
</StackLayout>
</ContentPage.Content>


C#



[ContentProperty("Content")]
[Preserve(AllMembers = true)]
public class StateCondition : View
{
public object Is { get; set; }
public object IsNot { get; set; }
public View Content { get; set; }
}

[ContentProperty("Conditions")]
[Preserve(AllMembers = true)]
public class StateContainer : ContentView
{
public List<StateCondition> Conditions { get; set; } = new List<StateCondition>();

// https://forums.xamarin.com/discussion/102024/change-bindableproperty-create-to-bindableproperty-create
//public static readonly BindableProperty StateProperty = BindableProperty.Create(nameof(State), typeof(object), typeof(StateContainer), propertyChanged: StateChanged);

public static readonly BindableProperty StateProperty = BindableProperty.Create<StateContainer, object>(x => x.State, null, propertyChanged: StateChanged);

public static void Init()
{
}

private static void StateChanged(BindableObject bindable, object oldValue, object newValue)
{
var parent = bindable as StateContainer;
parent?.ChooseStateProperty(newValue);
}

public object State
{
get { return GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}

private void ChooseStateProperty(object newValue)
{
foreach (StateCondition stateCondition in Conditions)
{
if (stateCondition.Is != null)
{
if (stateCondition.Is.ToString().Equals(newValue.ToString()))
{
Content = stateCondition.Content;
}
}
else if (stateCondition.IsNot != null)
{
if (!stateCondition.IsNot.ToString().Equals(newValue.ToString()))
{
Content = stateCondition.Content;
}
}
}
}
}









share|improve this question















I am using a basic State Management system found here: (code below)



https://github.com/xDelivered-Patrick/Xamarin.Forms.Essentials/blob/master/Essentials/Controls/State/StateContainer.cs



When implementing, my XAML View looks like the below.



The problem is the StackLayout CenterAndExpand is not working. All the views when rendered are simply scrunched at the top.



If I pull the particular section out of the StateContainer and eliminate its usage, it renders correctly. Is this an issue with the way the View is being rendered inside the Content of the StateContainer?



EDIT: Here is a minimal example: https://github.com/aherrick/StateManagementDemo



XAML:



<ContentPage.Content>

<StackLayout x:Name="layoutWrap" VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">

<controls:StateContainer State="{Binding State}">
<controls:StateCondition Is="Loading">

<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">

<ActivityIndicator x:Name="loadingIndicator"
IsVisible="{Binding IsBusy}"
IsRunning="{Binding IsBusy}"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand" />
</StackLayout>
</controls:StateCondition>

<controls:StateCondition Is="Loaded">
<!-- actual content here -->
</controls:StateCondition>
<controls:StateCondition Is="Error">

<StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">

<Label FontAttributes="Bold" Text="Oops! There was a problem." VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />
<Button Text="Tap to Retry" Command="{Binding LoadVenuesCommand}" CommandParameter="true" HorizontalOptions="CenterAndExpand" TextColor="White" Padding="15" BackgroundColor="#26265E" />
</StackLayout>
</controls:StateCondition>
</controls:StateContainer>
</StackLayout>
</ContentPage.Content>


C#



[ContentProperty("Content")]
[Preserve(AllMembers = true)]
public class StateCondition : View
{
public object Is { get; set; }
public object IsNot { get; set; }
public View Content { get; set; }
}

[ContentProperty("Conditions")]
[Preserve(AllMembers = true)]
public class StateContainer : ContentView
{
public List<StateCondition> Conditions { get; set; } = new List<StateCondition>();

// https://forums.xamarin.com/discussion/102024/change-bindableproperty-create-to-bindableproperty-create
//public static readonly BindableProperty StateProperty = BindableProperty.Create(nameof(State), typeof(object), typeof(StateContainer), propertyChanged: StateChanged);

public static readonly BindableProperty StateProperty = BindableProperty.Create<StateContainer, object>(x => x.State, null, propertyChanged: StateChanged);

public static void Init()
{
}

private static void StateChanged(BindableObject bindable, object oldValue, object newValue)
{
var parent = bindable as StateContainer;
parent?.ChooseStateProperty(newValue);
}

public object State
{
get { return GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}

private void ChooseStateProperty(object newValue)
{
foreach (StateCondition stateCondition in Conditions)
{
if (stateCondition.Is != null)
{
if (stateCondition.Is.ToString().Equals(newValue.ToString()))
{
Content = stateCondition.Content;
}
}
else if (stateCondition.IsNot != null)
{
if (!stateCondition.IsNot.ToString().Equals(newValue.ToString()))
{
Content = stateCondition.Content;
}
}
}
}
}






c# xaml xamarin xamarin.forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 13:01









Ashish Kamble

621419




621419










asked Nov 11 at 16:33









aherrick

9,2452286139




9,2452286139












  • No thoughts on this? I've included a minimal example of the issue above in my GitHub
    – aherrick
    Nov 17 at 13:58










  • Have you solved it?
    – Alexander Uddfeldt
    Nov 20 at 13:36










  • I ran a quick test on an android device. It seems to work as i described it bellow. Try it!
    – Alexander Uddfeldt
    Nov 20 at 13:51


















  • No thoughts on this? I've included a minimal example of the issue above in my GitHub
    – aherrick
    Nov 17 at 13:58










  • Have you solved it?
    – Alexander Uddfeldt
    Nov 20 at 13:36










  • I ran a quick test on an android device. It seems to work as i described it bellow. Try it!
    – Alexander Uddfeldt
    Nov 20 at 13:51
















No thoughts on this? I've included a minimal example of the issue above in my GitHub
– aherrick
Nov 17 at 13:58




No thoughts on this? I've included a minimal example of the issue above in my GitHub
– aherrick
Nov 17 at 13:58












Have you solved it?
– Alexander Uddfeldt
Nov 20 at 13:36




Have you solved it?
– Alexander Uddfeldt
Nov 20 at 13:36












I ran a quick test on an android device. It seems to work as i described it bellow. Try it!
– Alexander Uddfeldt
Nov 20 at 13:51




I ran a quick test on an android device. It seems to work as i described it bellow. Try it!
– Alexander Uddfeldt
Nov 20 at 13:51












1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted
+50










The solution is simple but not obvious. Add HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" to the <local:StateContainer> object as well. All Xamarin Forms LayoutOptions ending with ...AndExpand can olny expand if their containing parent allows it. That is why it worked when you removed the element from the <local:StateContainer> and put it directly in the StackLayout that allready had FillAndExpand declared thereby "allowing expansion".






share|improve this answer





















  • Of course! Thanks!
    – aherrick
    Nov 20 at 19:27










  • No Problem, happy to help!
    – Alexander Uddfeldt
    Nov 21 at 9:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250814%2fxamarin-forms-state-container-view-not-respecting-layout-options%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted
+50










The solution is simple but not obvious. Add HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" to the <local:StateContainer> object as well. All Xamarin Forms LayoutOptions ending with ...AndExpand can olny expand if their containing parent allows it. That is why it worked when you removed the element from the <local:StateContainer> and put it directly in the StackLayout that allready had FillAndExpand declared thereby "allowing expansion".






share|improve this answer





















  • Of course! Thanks!
    – aherrick
    Nov 20 at 19:27










  • No Problem, happy to help!
    – Alexander Uddfeldt
    Nov 21 at 9:19















up vote
5
down vote



accepted
+50










The solution is simple but not obvious. Add HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" to the <local:StateContainer> object as well. All Xamarin Forms LayoutOptions ending with ...AndExpand can olny expand if their containing parent allows it. That is why it worked when you removed the element from the <local:StateContainer> and put it directly in the StackLayout that allready had FillAndExpand declared thereby "allowing expansion".






share|improve this answer





















  • Of course! Thanks!
    – aherrick
    Nov 20 at 19:27










  • No Problem, happy to help!
    – Alexander Uddfeldt
    Nov 21 at 9:19













up vote
5
down vote



accepted
+50







up vote
5
down vote



accepted
+50




+50




The solution is simple but not obvious. Add HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" to the <local:StateContainer> object as well. All Xamarin Forms LayoutOptions ending with ...AndExpand can olny expand if their containing parent allows it. That is why it worked when you removed the element from the <local:StateContainer> and put it directly in the StackLayout that allready had FillAndExpand declared thereby "allowing expansion".






share|improve this answer












The solution is simple but not obvious. Add HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" to the <local:StateContainer> object as well. All Xamarin Forms LayoutOptions ending with ...AndExpand can olny expand if their containing parent allows it. That is why it worked when you removed the element from the <local:StateContainer> and put it directly in the StackLayout that allready had FillAndExpand declared thereby "allowing expansion".







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 13:49









Alexander Uddfeldt

2066




2066












  • Of course! Thanks!
    – aherrick
    Nov 20 at 19:27










  • No Problem, happy to help!
    – Alexander Uddfeldt
    Nov 21 at 9:19


















  • Of course! Thanks!
    – aherrick
    Nov 20 at 19:27










  • No Problem, happy to help!
    – Alexander Uddfeldt
    Nov 21 at 9:19
















Of course! Thanks!
– aherrick
Nov 20 at 19:27




Of course! Thanks!
– aherrick
Nov 20 at 19:27












No Problem, happy to help!
– Alexander Uddfeldt
Nov 21 at 9:19




No Problem, happy to help!
– Alexander Uddfeldt
Nov 21 at 9:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250814%2fxamarin-forms-state-container-view-not-respecting-layout-options%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Coverage of Google Street View

Full-time equivalent

Surfing