User avatar
b1qb0ss
Topic Author
Posts: 19
Joined: 09 Feb 2015, 15:10

[Unity] simplest way to have a messagebox

28 Dec 2015, 13:51

i want to know what is the simplest way to get a popup messagebox with OK / Cancel buttons

Thanks in advance
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: [Unity] simplest way to have a messagebox

07 Jan 2016, 15:16

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.
<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> 
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.
<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> 
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: [Unity] simplest way to have a messagebox

07 Jan 2016, 16:59

This is a related thread:

viewtopic.php?f=3&t=612&p=3290
 
User avatar
b1qb0ss
Topic Author
Posts: 19
Joined: 09 Feb 2015, 15:10

Re: [Unity] simplest way to have a messagebox

07 Jan 2016, 17:46

great thank you
 
User avatar
b1qb0ss
Topic Author
Posts: 19
Joined: 09 Feb 2015, 15:10

Re: [Unity] simplest way to have a messagebox

08 Jan 2016, 10:20

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
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: [Unity] simplest way to have a messagebox

08 Jan 2016, 11:33

Please, paste the XAML and CS here to investigate what is happening.

Thanks!
 
User avatar
b1qb0ss
Topic Author
Posts: 19
Joined: 09 Feb 2015, 15:10

Re: [Unity] simplest way to have a messagebox

08 Jan 2016, 12:13

XML
<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>
C#
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;
    }
}
 
User avatar
b1qb0ss
Topic Author
Posts: 19
Joined: 09 Feb 2015, 15:10

Re: [Unity] simplest way to have a messagebox

08 Jan 2016, 12:14

and this is how i'm using the MessageBox
           
 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);
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: [Unity] simplest way to have a messagebox

08 Jan 2016, 12:21

The problem is that the name given in the XAML and the one used in FindName does not match.
<Grid x:Name="LayoutRoot" Background="#80000000">
mainWindowRoot = (Grid)root.FindName("xpGrid");
It should be
mainWindowRoot = (Grid)root.FindName("LayoutRoot");
 
User avatar
b1qb0ss
Topic Author
Posts: 19
Joined: 09 Feb 2015, 15:10

Re: [Unity] simplest way to have a messagebox

08 Jan 2016, 12:33

The problem is that the name given in the XAML and the one used in FindName does not match.
<Grid x:Name="LayoutRoot" Background="#80000000">
mainWindowRoot = (Grid)root.FindName("xpGrid");
It should be
mainWindowRoot = (Grid)root.FindName("LayoutRoot");
i have 2 Grids the xpGrid is one under a viewbox and i'm loading the LayoutRoot in another variable

anynways i fixed it using DelegateCommand

thank you so much NoesisGUI is Awesome

Who is online

Users browsing this forum: No registered users and 1 guest