[Unity] simplest way to have a messagebox
i want to know what is the simplest way to get a popup messagebox with OK / Cancel buttons
Thanks in advance
Thanks in advance
-
-
sfernandez
Site Admin
- Posts: 3222
- Joined:
Re: [Unity] simplest way to have a messagebox
Hi,
I would suggest to create a UserControl that shows the dialog with the title, message and buttons. The user control will have a transparent/semitransparent background panel that will avoid clicking the controls below.
To show the dialog you will add the user control to a container that covers the entire screen and that is on top of any other control.
I would suggest to create a UserControl that shows the dialog with the title, message and buttons. The user control will have a transparent/semitransparent background panel that will avoid clicking the controls below.
Code: Select all
<UserControl
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
x:Class="MessageBoxControl">
<Grid Background="#40000000">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25*"/>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="25*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25*"/>
<RowDefinition Height="50*"/>
<RowDefinition Height="25*"/>
</Grid.RowDefinitions>
<Border x:Name="dialog" Grid.Column="1" Grid.Column="1" ...>
... message dialog here
</Border>
</Grid>
</UserControl>
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005">
<Grid x:Name="LayoutRoot">
... normal controls placed here
</Grid>
<Grid x:Name="MessageBoxContainer">
... message boxes added here
</Grid>
</Grid>
Re: [Unity] simplest way to have a messagebox
i was wondering how to add Click event for the button i used FindName to find the button but i'm getting
NullReferenceException: Object reference not set to an instance of an object
NullReferenceException: Object reference not set to an instance of an object
Re: [Unity] simplest way to have a messagebox
Please, paste the XAML and CS here to investigate what is happening.
Thanks!
Thanks!
Re: [Unity] simplest way to have a messagebox
XML
C#
Code: Select all
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MessageBox">
<Grid x:Name="LayoutRoot" Background="#80000000">
<Border BorderBrush="Black" BorderThickness="1"
HorizontalAlignment="Center" VerticalAlignment="Center" Margin="200,100">
<StackPanel>
<Border Background="Gray" Padding="5,2">
<TextBlock Text="{Binding Title}" FontWeight="Bold" FontSize="14" />
</Border>
<Border Padding="5">
<TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="14" />
</Border>
<StackPanel x:Name="ButtonsPanel" Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
<Button Content="{Binding OkText}" Command="{Binding OkCommand}" />
<Button Content="{Binding CancelText}" Command="{Binding CancelCommand}"/>
</StackPanel>
</StackPanel>
</Border>
</Grid>
</UserControl>
Code: Select all
using System;
using Noesis.UserControls;
using Noesis;
[UserControlSource("Assets/UI/MessageBox.xaml")]
public class MessageBox : UserControl
{
public static DependencyProperty TitleProperty = DependencyProperty.Register(
"Title", typeof(string), typeof(MessageBox), new PropertyMetadata(""));
public string Title
{
get { return GetValue(TitleProperty).ToString(); }
set { SetValue(TitleProperty, value); }
}
public static DependencyProperty MessageProperty = DependencyProperty.Register(
"Message", typeof(string), typeof(MessageBox), new PropertyMetadata(""));
public string Message
{
get { return GetValue(MessageProperty).ToString(); }
set { SetValue(MessageProperty, value); }
}
public static DependencyProperty OkTextProperty = DependencyProperty.Register(
"OkText", typeof(string), typeof(MessageBox), new PropertyMetadata(""));
public string OkText
{
get { return GetValue(OkTextProperty).ToString(); }
set { SetValue(OkTextProperty, value); }
}
public static DependencyProperty CancelTextProperty = DependencyProperty.Register(
"CancelText", typeof(string), typeof(MessageBox), new PropertyMetadata(""));
public string CancelText
{
get { return GetValue(CancelTextProperty).ToString(); }
set { SetValue(CancelTextProperty, value); }
}
public void OnPostInit()
{
Grid root = (Grid)FindName("LayoutRoot");
root.DataContext = this;
}
}
Re: [Unity] simplest way to have a messagebox
and this is how i'm using the MessageBox
Code: Select all
var gui = GetComponent<NoesisGUIPanel>();
Grid root = (Grid)gui.GetContent();
mainWindowRoot = (Grid)root.FindName("xpGrid");
MessageBox msgBox = new MessageBox
{
Title = "Quit?",
Message = "Do you really want to quit?",
OkText = "Yes!",
CancelText = "Cancel",
};
mainWindowRoot.Children.Add(msgBox);
Re: [Unity] simplest way to have a messagebox
The problem is that the name given in the XAML and the one used in FindName does not match.
It should be
Code: Select all
<Grid x:Name="LayoutRoot" Background="#80000000">
Code: Select all
mainWindowRoot = (Grid)root.FindName("xpGrid");
Code: Select all
mainWindowRoot = (Grid)root.FindName("LayoutRoot");
Re: [Unity] simplest way to have a messagebox
i have 2 Grids the xpGrid is one under a viewbox and i'm loading the LayoutRoot in another variableThe problem is that the name given in the XAML and the one used in FindName does not match.Code: Select all<Grid x:Name="LayoutRoot" Background="#80000000">
It should beCode: Select allmainWindowRoot = (Grid)root.FindName("xpGrid");
Code: Select allmainWindowRoot = (Grid)root.FindName("LayoutRoot");
anynways i fixed it using DelegateCommand
thank you so much NoesisGUI is Awesome
Who is online
Users browsing this forum: Semrush [Bot] and 5 guests