StackLayout isVisible property not working in Xamarin Forms - popup

I am trying to display popup using AbsoluteLayout. On a button click, Iam setting stacklayout as visible. But its not displayed.
<AbsoluteLayout x:Name="absoluteLayout">
<StackLayout x:Name="layout1"
BackgroundColor="White"
Spacing="1"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All">
<StackLayout>
<StackLayout x:Name="popupLayout"
BackgroundColor="Gray"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All"
IsVisible="False"
Spacing="0">
//Content
</StackLayout>
</AbsoluteLayout>
On a button click , Setting visibility to true
public void OnButtonClick(object sender, EventArgs args){
popupLayout.IsVisible = true;
}
I tried to set visibility of popupLayout to false in OnAppearing and then enabling on button click, still having same behaviour.
Update : Added code detail. By setting background to popupLayout,I came to know that on button click layout is visible, but its content / children are not displayed. Should we have to enable child views / elements separately?

Try this
public void OnButtonClick(object sender, EventArgs args){
popupLayout.IsVisible = true;
popupLayout.ForceLayout();
}
Or
public void OnButtonClick(object sender, EventArgs args){
popupLayout.IsVisible = true;
popupLayout.Parent.ForceLayout();
}

Related

Custom control - event handler to main app when inside value changed

I want to be able to add an event from my custom control to the outside world
as I change a value in my custom control, I use TwoWay, but I also want an event to be generated in my main app
I just cannot find anything relevant to this
the main app xaml contains this line :
<custom:MyComp x:Name="myComp" ValueChanged="SomeFunction" Value="{ Binding Path=someIntVariable, Mode=TwoWay}"></custom:MyComp>
how do I implement a ValueChanged event in the custom control ?
my control xaml:
<UserControl ...>
<StackPanel Orientation="Horizontal" >
...
<TextBox x:Name="MyCompTextBox" Width="50" Height="20" TextChanged="MyCompTextBox_TextChanged"/>
...
</StackPanel>
</UserControl>
the code
public partial class MyComp: UserControl
{
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(MyComp), new FrameworkPropertyMetadata(0, OnValuePropertyChangedCallback));
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
private static void OnValuePropertyChangedCallback(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
MyComp mMyComp= source as MyComp;
int newValue=(int)e.NewValue;
myComp.MyCompTextBox.Text = newValue.ToString();
}
public MyComp()
{
InitializeComponent();
}
private void MyCompTextBox_TextChanged(object sender, EventArgs e)
{
int _bpm = int.ParseMyCompTextBox.Text);
Value = _bpm;
MyCompTextBox.Text = Value.ToString();
// here I want to trigger an even to the main app !!!
}
}
thanks for your help

How to share information between pages

Before starting, as is stated in the title, I'm learning .NET MAUI and I'm very new in this.
My problem is that I can't find a way to share information from a page to a previous page.
What I'm trying to do is the following:
In the MainPage, have a button that once pressed, sends the user to another page, let's call it LoginPage, there, ask the user for his name, and after the input, redirect him to the previous page showing an "Hello {Name}, and welcome!"
MainPage code:
string Name = "";
string greetings = "";
async private void LogintBtn_Clicked(object sender, EventArgs e)
{
{
await Navigation.PushAsync(new LoginPage());
}
greetings = $"Welcome {Name}!";
Greetinglbl.Text = greetings;
}
Log in code:
public string name { get; set; }
private void btnRegister_Clicked(object sender, EventArgs e)
{
if (ValidateName()==false) { return; };
Navigation.PopAsync();
}
private bool ValidateName()
{
if (string.IsNullOrWhiteSpace(txtRegistro.Text))
{
return false;
}
else
return true;
}
Beside this, I don't know how to share information between these pages.
I have seen that a lot of devs use MVVM but I can't get a Beginners guide propperly, even watching the .NET MAUI Beginners Guides from James Montemagno doesn't help me.
I've also tried using something like
await Shell.Current.GoToAsync($"LoginPage?Name={Name}");
that, for what I read is used to send data but don't know where to put it, plus triying to navigate with the same line throws an error (null).
Plus, most of the videos only shows how to send Data to the next Page, not the previous, and usually to a collection, and some of them import NuGet Packages that I don't see to be necessary in this project.
In your scenario, you could share the data through Constructor, here's the code snippet below for your reference.
MainPage:
Xaml:
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Label Text="MainPage"></Label>
<Button
Text="To Login Page"
Clicked="OnClicked"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ScrollView>
Code-behind:
      public MainPage()
      {
            InitializeComponent();
      }
public MainPage(string a)
{
InitializeComponent();
      
            App.Current.MainPage.DisplayAlert("Welcome Back",a,"OK");
}
private async void OnClicked(object sender, EventArgs e)
      {
            await Navigation.PushAsync(new LoginPage());
      }
LoginPage:
Xaml:
<VerticalStackLayout>
<Label Text="LoginPage"></Label>
<Entry x:Name="myentry"/>
<Button
Text="ToMain"
Clicked="BackTo"
HorizontalOptions="Center" />
</VerticalStackLayout>
Code-behind:
public LoginPage()
      {
            InitializeComponent();
      }
private async void BackTo(object sender, EventArgs e)
{
await Navigation.PushModalAsync(new MainPage(myentry.Text));
}
Last but not least, in your App.xaml.cs, use it like below:
MainPage = new NaviagtionPage(new MainPage())

Xamarin.Forms Form.Children.Add for ContentPage

I would like to have a few Content Pages , which I add in my MainPage class. I use SetLayout function to do that, which parameter is ContentPage. How to dock a ContentPage to my ContentPage( MainPage) as children please?
My MainPage xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp.MainPage"
x:Class="MyApp;assembly=App"
BackgroundColor="White">
<StackLayout x:Name="Form">
</StackLayout>
And SetLayout function in MainPage.cs
private void SetLayout(ContentPage page)
{
if (LastControl != null)
{
LastControl.IsEnabled = false;
Form.Children.Remove(LastControl);
Form.Children.Add(page);
}
else
{
Form.Children.Add(page);
}
page.IsVisible = true;
LastControl = page;
}
In function above I have error on Form.Children.Add, because this wants contentview, not contentpage.
I dont want to change my ContentPage to ContentView, because I want to have working BackButton.
Any idea? Thanks

ascx viewstate null when inside hidden PlaceHolder

I have a custom user control ascx that exposes a simple property
/// <summary>
/// The currently selected ID, if there is one
/// </summary>
public virtual int? SelectedId
{
get { return (int)ViewState["XXID"]; }
set { ViewState["XXID"] = value; }
}
The control is inside a PlaceHolder and the value reads just fine in the postback onclick method of a button below the PlaceHolder.
However if the PlaceHolder visible=false then ViewState["XXID"] returns null. If I toggle the PlaceHolder visible=true then the value comes back.
<asp:PlaceHolder runat="server" ID="plcCustomer" >
<my:CustomPicker runat="server" ID="cboCustomer" />
</asp:PlaceHolder >
<asp:Button runat="server" ID="btnToggleVisible" onclick="btnToggleVisible_OnClick" text="Toggle visible" />
<asp:Button runat="server" ID="btnGetSelectedId" onclick="btnGetSelectedId_OnClick" text="Get Value" />
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
plcCustomer.SelectedId = 5;
}
protected void btnToggleVisible_OnClick(object sender, EventArgs e)
{
plcCustomer.Visible = !plcCustomer.Visible;
}
protected void btnGetSelectedId_OnClick(object sender, EventArgs e)
{
...
plcCustomer.SelectedId //<== this will be null whenever plcCustomer is invisible
...
}
I have a hunch that the page has not bothered loading my control's viewstate because it is not going to be visible.
If so how can I instruct the page that it should load the viewstate of custom controls even when they are in a hidden PlaceHolder?

printdialogue from view model in wpf

I have a requirement as follows, I want to print the screen elements present on the screen to printer. Implementation is done through MVVM. so If I click on print button on the screen it should display a print dialogue and selecting the printer should proceed with printing all the UI elemnts with their data . I have tried with solution present at print WPF visual from viewmodel but its missing the margings and not displaying properly
Also I have another button Print Preview which should display print preview dialogue to see the preiview.
Thanks in advance.
Regards,
Krishna.
In my opinion the printing of the View in an MVVM application is not the responsiblity or concern of the ViewModel. I believe you are better of doing this from the View.
How I've achieved this before is to use a WPF Behavior on a button - I use a Behavior because I'm using DataTemplates for the View and there isn't a 'code behind' file.
The Behavior exposes a DependencyProperty, this is a binding to what is to be printed or contains what is going to be printed.
XAML:
<Button Margin="0,2,5,2"
HorizontalAlignment="Right"
Content="PRINT"
ToolTip="Prints the current report">
<i:Interaction.Behaviors>
<b:ReportPrintClickBehavior Content="{Binding ElementName=SelectedReportContent, Mode=OneWay}" />
</i:Interaction.Behaviors>
</Button>
To reference the Behavior in the XAML you'll need to reference System.Windows.Interactivity, this can be found on NuGet here.
Code-Behind (Behavior):
In this case I'm printing a FlowDocument hosted inside a FlowDocumentReader.
public sealed class ReportPrintClickBehavior : Behavior<Button>
{
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content",
typeof(DependencyObject),
typeof(ReportPrintClickBehavior),
new PropertyMetadata(null));
public DependencyObject Content
{
get { return (DependencyObject)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += OnLoaded;
AssociatedObject.Unloaded += OnUnloaded;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Loaded -= OnLoaded;
AssociatedObject.Unloaded -= OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs args)
{
AssociatedObject.Click += OnClick;
}
private void OnUnloaded(object sender, RoutedEventArgs args)
{
AssociatedObject.Click -= OnClick;
}
private void OnClick(object sender, RoutedEventArgs args)
{
var flowDocumentReader = Content.GetVisualDescendent<FlowDocumentReader>();
if (flowDocumentReader != null)
{
flowDocumentReader.Print();
}
}
}