I have separated two projects in my solution because they each require libraries targeting different CPU.
In one of my project, I just have classes that respond to clicks (let's call it ProjectClick 64 bits libraries), the other one is a sort of UI with an MVVM implementation (ProjectUser 32 bits libraries).
The thing I am searching for is a way to let the ProjectUser know that the click has been performed by the ProjectClick, without the Project Click knowing anything else.
What I have tried so far
I have been scattering the web and books to understand a bit more about C#. From what I understood, to communicate, the best way is to create a Interface. I have been looking at this subject for an answer, and have been trying to implement a third project with an interface between the two.
Ok, here goes the code, (this is a purposely simplified code, I hope it is clear enough)
First the Interface (in a console application)
namespace LinkApplication
{
public interface IEvent
{
bool CompareClick { get; set; }
}
}
Then, the project clicking which is a wpf
namespace ProjectClick
public partial class MainWindow : Window, IEvent
{
public MainWindow()
{
try { InitializeComponent(); }
catch (Exception e)
{
Console.WriteLine(e.InnerException);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
CompareClick = true;
}
private void Button_Leave(object sender, RoutedEventArgs e)
{
CompareClick = false;
}
}
Finally the UI
namespace ProjectUser
{
public partial class MainWindow : Window, IEvent, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen; //start the window at the centre of the screen
DataContext = this;
}
public bool CompareClick { get; set; }
public bool ClickCheck
{
get { return CompareClick; }
set
{
if (value != CompareClick)
{
CompareClick = value;
OnPropertyChanged("ClickCheck");
}
}
}
You can see the realted Label here in the Window
<Label Content="{Binding ClickCheck}" HorizontalAlignment="Left" Margin="690,358,0,0" VerticalAlignment="Top"/>
Here, the value always stays at false, and I don't really understand the logic of the changing value. I am still learning, and I have seen several other ideas on the web like a custom EventHandler, but I don't really understand the implementation between two projects not knowing each others. I will be glad if someone could route me towards a possible solution, or a better way to perform.
Edit
I would preferably like to avoid referring the Project Click in the ProjectUser to keep the privileges of different CPU targeting. The other way around is not a problem.
Thank you for your kind answers.
I have been greatly advised and have looked into Inter Process Communication between instances. I have looked into different things but the most satisfying answer of all was on Omegaman's blog (bit thanks to this subject).
So basically, I have tried to avoid localhost information, thinking there would be a more straightforward solution. But since we have not thought of anything better, I think this is what I was looking for.
What I have found
So now, the solution here was to use a WCF service with NamedPipes. By creating a Sender and Receiver actions, the two process ProjectUser and ProjectClick never encounter each other directly. You have instead a pipe controlled by the WCF. You can see more details on the blog on how to communicate, I just adapted (without great change) what he did by changing the passing information.
One thing to note however
The processes cannot both start at the same time, and the receiver must start first to listen to the information coming through. Basically, the sender has to start afterwards.
I created two windows in WPF, and a WCFServiceLibrary. When the button is clicked, there is an incrementation, and it shows the number on the second screen.
A little bit of code
You can see a lot on Omegaman's blog, and I will just post what I have changed.
On the ProjectUser side, supposed to receive, the label is updated as follows
Receiver pipe = new Receiver();
public MainWindow()
{
InitializeComponent();
//this.WindowStartupLocation = WindowStartupLocation.CenterScreen; //start the window at the centre of the screen
DataContext = this;
pipe.Data += new PipeLink.PipeService.DataIsReady(DataBeingRecieved);
if (pipe.ServiceOn() == false)
MessageBox.Show(pipe.error.Message);
label1.Content = "Listening to Pipe: " + pipe.CurrentPipeName + Environment.NewLine;
}
void DataBeingRecieved(int data)
{
Dispatcher.Invoke(new Action(delegate()
{
label1.Content += string.Join(Environment.NewLine, data);
label1.Content += Environment.NewLine;
}));
}
On the ProjectClick side, supposed to send, the button click updates as follows
int i;
public MainWindow()
{
try { InitializeComponent(); }
catch (Exception e)
{
Console.WriteLine(e.InnerException);
}
i = 0;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
int messages;
i++;
Stopwatch stoop = new Stopwatch();
stoop.Start();
messages = i;
try
{
PipeLink.Sender.SendMessage(messages);
stoop.Stop();
Console.WriteLine(stoop.ElapsedMilliseconds + " ms");
}
catch (Exception u)
{
Console.WriteLine(u);
}
}
The important part of the code, is the creation of the pipe itself, using NetNamedPipeBinding. This is where the whole communication will take place
You can see it in the PipeService code :
public class PipeService : IPipeService
{
public static string URI
= "net.pipe://localhost/Pipe";
// This is when we used the HTTP bindings.
// = "http://localhost:8000/Pipe";
#region IPipeService Members
public void PipeIn(int data)
{
if (DataReady != null)
DataReady(data);
}
public delegate void DataIsReady(int hotData);
public DataIsReady DataReady = null;
#endregion
}
What about the speed?
I was afraid simple data may take longer to arrive than on a simple click. I was mistaken : the first number took longer than the others because of the first connection, so about a second. But after that, for clicking about a 100 times, I had a, average of 10 ms (I know it is not significant data, still I thought it was good to test it a couple of times).
I am pushing everything on the GitHub used with Andreas, for anyone who might be interested.
I still do not know if the code is optimized though. Should you have a better solution, I will happily read it.
As others pointed out your concept of interfaces is wrong still. However i get what you're trying to do.
Try this:
namespace LinkApplication
{
public interface IEventReceiver
{
void Receive<T>(T arg) where T : EventArgs;
}
public class SomeUniqueEvent : EventArgs
{
public bool Clicked { get; set; }
public SomeUniqueEvent(bool clicked)
{
Clicked = clicked;
}
}
public static class EventTunnel
{
private static readonly List<IEventReceiver> _receivers = new List<IEventReceiver>();
public static void Publish<T>(T arg) where T : EventArgs
{
foreach (var receiver in _receivers)
{
receiver.Receive(arg);
}
}
public static void Subscribe(IEventReceiver subscriber)
{
_receivers.Add(subscriber);
}
}
}
namespace ProjectClick
{
public partial class MainWindow : Window
{
public MainWindow()
{
try { InitializeComponent(); }
catch (Exception e)
{
Console.WriteLine(e.InnerException);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
LinkApplication.EventTunnel.Publish(new LinkApplication.SomeUniqueEvent(true));
}
private void Button_Leave(object sender, RoutedEventArgs e)
{
LinkApplication.EventTunnel.Publish(new LinkApplication.SomeUniqueEvent(false));
}
}
}
namespace ProjectUser
{
public partial class MainWindow : Window, LinkApplication.IEventReceiver, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen; //start the window at the centre of the screen
DataContext = this;
LinkApplication.EventTunnel.Subscribe(this);
}
public bool CompareClick { get; set; }
public bool ClickCheck
{
get { return CompareClick; }
set
{
if (value != CompareClick)
{
CompareClick = value;
OnPropertyChanged("ClickCheck");
}
}
}
public void Receive<T>(T arg) where T : EventArgs
{
var casted = arg as SomeUniqueEvent;
if (casted != null)
{
ClickCheck = casted.Clicked;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
Here, you misunderstand what an interface is. Every implementation of an interface is a different one. When you click the button, CompareClick property of ProjectClick project's MainWindow changes value. But that doesn't change the ProjectUser project's MainWindow. They are two completely different objects! The best way that I can think of now, is to make the button public. Alternatively, you can create a method in the MainWindow class of the ProjectClick. Use this method to subscribe to the click event. Something like this:
public void SubscribeToClickEvent (EventHandler handler) {
this.Button.Click += handler //whatever your button is called
}
If you want to encapsulate Button, use the method above. If you don't, then just make it public.
And you ask, how can I access an instance of MainWindow to use the method? The only way I can think of is to make MainWindow a singleton.
Related
Hi I'm new to WPF and XAML, I'm trying to utilize MVVMCross's MvxInteraction to interact with the user to get a "YES" or "NO" confirmation based off this example.
I've been hitting a snag on getting the interaction to subscribe to an event handler as the interaction is always null. I can see that from the references that the interaction variable see each other based on the binding, so I'm not sure what's going on. I've looked around and found this, that states for me to bring my binding later into my UserControl View behind code, so I used a dispatcher, but that did not work either.
VIEW MODEL
public class StudentDetailsViewModel : MvxViewModel
{
private InteractionRequest<YesNoQuestion> _interaction = new InteractionRequest<YesNoQuestion>();
public IInteractionRequest Interaction => _interaction;
}
VIEW.XAML.CS
public partial class StudentDetailsView : MvxWpfView
{
private InteractionRequest<YesNoQuestion> _interaction;
public StudentDetailsView()
{
InitializeComponent();
Dispatcher.BeginInvoke(new Action(() => BindInteractions()), DispatcherPriority.ContextIdle, null);
}
public InteractionRequest<YesNoQuestion> Interaction
{
get => _interaction;
set
{
if(_interaction != null)
{
_interaction.Requested -= OnInteractionRequested;
}
_interaction = value;
_interaction.Requested += OnInteractionRequested; //***RUN TIME NULL EXCEPTION***
}
}
private void OnInteractionRequested(object sender, InteractionRequestedEventArgs eventArgs)
{
var yesNoQuestion = eventArgs.Callback;
}
private void BindInteractions()
{
var set = this.CreateBindingSet<StudentDetailsView, StudentDetailsViewModel>();
set.Bind(this).For(view => view.Interaction).To(viewModel => viewModel.Interaction).OneWay();
set.Apply();
}
}
INTERACTION CLASS
public class YesNoQuestion
{
public bool? Confirmation { get; set; }
public string Question { get; set; }
public YesNoQuestion(string message)
{
Question = message;
}
}
My second question is that I'm a little confused on what they implemented with the "ShowDialog" and "DialogStatus" here within their example:
private async void OnInteractionRequested(object sender, MvxValueEventArgs<YesNoQuestion> eventArgs)
{
var yesNoQuestion = eventArgs.Value;
// show dialog
var status = await ShowDialog(yesNoQuestion.Question);
yesNoQuestion.YesNoCallback(status == DialogStatus.Yes);
}
Are they simply calling upon another usercontrol view to show itself through a ShowDialog Method?
_interaction.Requested += OnInteractionRequested; //***RUN TIME NULL EXCEPTION***
Somehow this is always null on the first startup, and then it will assign the proper interaction later, so add a null check to solve this. Maybe we need to confirm with MVVMCross itself.
Second, you can handle whatever you want to display on interaction request, for example, shows MessageBox with yes no button type or pop another view to display custom message box one. Since this runs on the WPF layer.
public partial class UserControl1 : UserControl,IRequireGraphicInterface
{
private void button1_Click(object sender, RoutedEventArgs e)
{
// int i;
Opcconnect OC = new Opcconnect();
OC.DataRead();
txtBox4.Text = "zjy";
}
}
public partial class Opcconnect : OPCServerClass
{
public void DataRead()
{
UserControl1 TxtgetData = new UserControl1();
try
{
TxtgetData.txtBox2.Text = "SJZ";
TxtgetData.txtBox3.Text ="TEST"
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
//I'm beginner, I have tested my program and show noting. I hope get your help how could I get the txtBox2.Text and txtBox3.Text value. thanks.
You've created an instance of UserControl1, but you haven't done anything with that instance. It's not part of any form, it's not displayed to the user, etc. You are successfully setting the values, but never showing those values to the user. Because that instance has nothing to do with any other instance, such as the one you're currently seeing on your screen.
Take a step back. Your Opcconnect class should not be trying to directly set UI controls anyway. It should simply return the data needed by the UI, and the form code should update the controls. For example:
public partial class Opcconnect : OPCServerClass
{
public Tuple<string, string> DataRead()
{
return new Tuple<string, string>("SJZ", "TEST");
}
}
(I've used a Tuple<T1,T2> here because without any context there's no way to know what data structure would be better appropriate. It's likely you'd want a custom class for this. But for now this will at least return two string values.)
Then in your form code you'd get those values and update your controls:
public partial class UserControl1 : UserControl,IRequireGraphicInterface
{
private void button1_Click(object sender, RoutedEventArgs e)
{
Opcconnect OC = new Opcconnect();
var values = OC.DataRead();
txtBox2.Text = values.Item1;
txtBox3.Text = values.Item2;
txtBox4.Text = "zjy";
}
}
Basically, keep your UI code in the UI. Different layers of the application (UI, business logic, database and infrastructure dependencies) shouldn't leak their implementations across layer boundaries, they should consume/return just the data being transferred.
First off, I'm using Xamarin Studio 6.1.3 in case that makes any difference.
I'm creating a simple app and want a login form to appear as a sheet. I followed the Xamarin tutorial for creating sheets (https://developer.xamarin.com/guides/mac/user-interface/working-with-dialogs/#Creating_a_Custom_Sheet) but am running into an issue.
Per the tutorial, I have created a class:
using System;
using Foundation;
using AppKit;
namespace SampleProject
{
public partial class UserLoginController : NSViewController
{
private NSViewController _presentor;
public string Username
{
get { return TxtUsername.StringValue; }
set { TxtUsername.StringValue = value; }
}
public string Password
{
get { return TxtPassword.StringValue; }
set { TxtPassword.StringValue = value; }
}
public NSViewController Presentor
{
get { return _presentor; }
set { _presentor = value; }
}
public UserLoginController(IntPtr handle) : base(handle)
{
}
private void CloseDialog()
{
Presentor.DismissViewController(this);
}
partial void BtnCancelClick(NSObject sender)
{
RaiseDialogCanceled();
CloseDialog();
}
partial void BtnLoginClick(NSObject sender)
{
RaiseDialogAccepted();
CloseDialog();
}
public EventHandler DialogAccepted;
internal void RaiseDialogAccepted()
{
if (this.DialogAccepted != null)
this.DialogAccepted(this, EventArgs.Empty);
}
public EventHandler DialogCanceled;
internal void RaiseDialogCanceled()
{
if (this.DialogCanceled != null)
this.DialogCanceled(this, EventArgs.Empty);
}
}
}
And I have added an override for PrepareForSegue in my ViewController class:
public override void PrepareForSegue(NSStoryboardSegue segue, NSObject sender)
{
base.PrepareForSegue(segue, sender);
switch (segue.Identifier)
{
case "UserLoginSegue":
UserLoginController loginSheet = segue.DestinationController as UserLoginController;
loginSheet.Username = ""; //This line throws NullReferenceException unless I set a breakpoint and expand loginSheet.Base before allowing this line to execute.
loginSheet.Password = "";
loginSheet.Presentor = this;
loginSheet.DialogAccepted += (object s, EventArgs e) => { Console.WriteLine("OK Clicked"); };
loginSheet.DialogCanceled += (object s, EventArgs e) => { Console.WriteLine("Cancel Clicked"); };
break;
}
}
See the comment in the above code block. I basically set a breakpoint on that line and when it triggers, I inspect the loginSheet object. If I expand the Base object to inspect it and then continue execution, everything works as expected. If I don't, I get a NullReferenceException whenever code tries to access any fields/properties/methods in the UserLoginController class.
I am completely baffled as to why this is happening. I set a breakpoint in the constructor of UserLoginController and verified it is being called with a handle and that the base constructor should be called as well.
I've read through the tutorial several times and don't see anything that I'm missing. Can't seem to find anybody else having the same problem.
My ultimate question is: What can I do to make the code work as expected?
For the sake of learning (which may shed light on the problem): What is going on behind the scenes when I inspect the base object of my UserLoginController class while debugging?
When you expand an object in the debugger, all properties not marked up with certain attributes are read via reflection so that we can display them in the IDE.
Maybe one of those properties has a side effect? You should be able to reproduce the effect using reflection, then bisect the property list to see who's effecting your behavior.
The solution turned out to be that I need to check the View property of my UserLoginController.
I added the following line:
var theView = loginSheet.View;
and everything works as expected. I have yet to dig into the View property to see what it is doing behind the scenes that makes everything work.
Here is the modified, working PrepareForSegue override method:
public override void PrepareForSegue(NSStoryboardSegue segue, NSObject sender)
{
base.PrepareForSegue(segue, sender);
switch (segue.Identifier)
{
case "UserLoginSegue":
UserLoginController loginSheet = segue.DestinationController as UserLoginController;
var theView = loginSheet.View;
loginSheet.Username = "";
loginSheet.Password = "";
loginSheet.Presentor = this;
loginSheet.DialogAccepted += (object s, EventArgs e) => { Console.WriteLine("OK Clicked"); };
loginSheet.DialogCanceled += (object s, EventArgs e) => { Console.WriteLine("Cancel Clicked"); };
break;
}
}
I'd like to know what's the best way (read most elegant) to have a single instance of a given Window per application in WPF.
I'm a newcomer to .NET and WPF and what I came up with looks pretty lame.
private static readonly Object MUTEX = new Object();
private static AboutWindow INSTANCE;
public static AboutWindow GetOrCreate() {
lock (MUTEX) {
if (INSTANCE == null) {
INSTANCE = new AboutWindow();
}
INSTANCE.Show();
return INSTANCE;
}
}
private AboutWindow() {
InitializeComponent();
}
private void AboutWindow_Closed(object sender, EventArgs e) {
// the Closed events are handy for me to update values across
// different windows.
lock (MUTEX) {
INSTANCE = null;
}
}
Thing is... this looks like utter crap. There must be some way to achieve the same goal in a much more elegant way, right?
PS: I'm often using the Closed event to change values in other open windows. For instance I have the SettingsWindow with the "Account" button. When I push that button, the AccountWindow pops up. When I close AcountWindow, I want something in the SettingsWindow to change (a label). Hence the constant creation of windows.
Besides, Close is something you always have to deal with because of the X button on the window frame...
there are probably better ways to do this, but here is a relatively simple way....
put a static bool on your window class to flag if its open or not. then, in the load() event set it to true, and on the close event set it to false. Then, in the code that opens the window, check the flag.
here is some pseudo-code to give you an idea...
public class AboutWindow
{
public static bool IsOpen {get;private set;}
onLoadEvent(....)
{
IsOpen = true;
}
onUnloadEvent(...)
{
IsOpen = false;
}
}
public void OpenAbout()
{
if ( AboutWindow.IsOpen ) return;
AboutWindow win = new AboutWindow();
win.Show();
}
If you truly need to enforce a single instance of a window, then a static instance (some flavor of what you have) with a factory creation method is certainly a viable option, much like a single DataContext instance when working with a database.
You could also write your own WindowManager class, although that seems like overkill, and will essentially be the same thing (except the Factory methods would be in a single class).
However, re-reading your post, I wonder if this is a case of missing the forest for the trees. Your mentioning of your SettingsWindow, which in turn calls AccountWindow, makes me think that you should simply be using ShowDialog(). This opens a window modally, meaning that there can be no interaction with the calling window (or any other window in your application). You simply set a property in that dialog, set the DialogResult to true when the OK button is pressed, and read that property in the parent window.
Basically, you just use the ShowDialog like this. I am leaving out a lot of the implementation details, as far as binding vs. hard-coding to controls. Those details aren't as important as just seeing how ShowDialog works.
For simplicity, assume that you have a class called MyAppOptions that, well, reflect the options of your application. I will leave off most of the implementation details of this for simplicity, but it would likely implement INotifyPropertyChanged, have methods and fields and properties, etc.
public class MyAppOptions
{
public MyAppOptions()
{
}
public Boolean MyBooleanOption
{
get;
set;
}
public String MyStringOption
{
get;
set;
}
}
Then, let's make this simple, and assume that you want to show an Options dialog when you press a button on some window. Furthermore, I will assume that there are variables that have been set with your options, which were loaded at startup.
void btnOptions_Click(object sender, RoutedEventArgs e)
{
MyAppOptions options = new MyAppOptions();
options.MyBooleanOption = mSomeBoolean;
options.MyStringOption = mSomeString;
OptionsDialog optionsDialog = new optionsDialog(options);
if (optionsDialog.ShowDialog() == true)
{
// Assume this function saves the options to storage
// and updates the application (binding) appropriately
SetAndSaveOptions(optionsDialog.AppOptions);
}
}
Now assume that the OptionsDialog is a window you've created in your project, and it has a CheckBox on it related to MyBooleanOption and a TextBox for MyStringOption. It also has an Ok button and a Cancel button. The code-behind will likely use Binding, but for now we'll hard code the values.
public class OptionsDialog : Window
{
public OptionsDialog(MyAppOptions options)
{
chkBooleanOption.IsChecked = options.SomeBooleanOption;
txtStringOption.Text = options.SomeStringOption;
btnOK.Click += new RoutedEventHandler(btnOK_Click);
btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
}
public MyAppOptions AppOptions
{
get;
set;
}
void btnOK_Click(object sender, RoutedEventArgs e)
{
this.AppOptions.SomeBooleanOption = (Boolean) chkBooleanOption.IsChecked;
this.AppOptions.SomeStringOption = txtStringOption.Text;
// this is the key step - it will close the dialog and return
// true to ShowDialog
this.DialogResult = true;
}
void btnClose_Click(object sender, RoutedEventArgs e)
{
// this will close the dialog and return false to ShowDialog
// Note that pressing the X button will also return false to ShowDialog
this.DialogResult = false;
}
}
This is a pretty basic example as far as implementation details. Search online for ShowDialog for more details. The important keys to remember are:
ShowDialog opens a window modally,
meaning it is the only window in your
application that can be interacted
with.
Setting DialogResult to true
will close the dialog, which can be
checked for from the calling parent.
Setting DialogResult to false will
also close the dialog, in which case
you skip updating the values in the
calling window.
Pressing the X button
on the window automatically sets the
DialogResult to false
You can have public properties in the dialog window that can be set before doing the ShowDialog, and can get values from after the dialog disappears. It will be available while the dialog is still in scope.
The following extends on the above solution to reshow the window if it is already open. In this case it is a help window.
///<summary>
/// Show help from the resources for a particular control by contextGUID
///</summary>
///<param name="contextGUID"></param>
private void ShowApplicationHelp(string contextGUID = "1")
{
if (HelpWin != null)
{
if (HelpWin.IsOpen)
{
HelpWin.BringToFront();
return;
}
}
HelpWin = new MigratorHelpWindow();
HelpWin.Owner = Application.Current.MainWindow;
HelpWin.ResizeMode = ResizeMode.CanResizeWithGrip;
HelpWin.Icon = new Image()
{
Source =
new BitmapImage(
new Uri(
"pack://application:,,,/ResourceLibrary;component/Resources/Images/Menu/Help.png",
UriKind.RelativeOrAbsolute))
};
HelpWin.Show();
HelpWin.BringToFront();
}
This code is all in a viewmodel (MVVM) associated with the window. It is called by an ICommand hooked to a button on the window (naturally, it shows a question mark!!)
The following property is involved (in this case it is a Telerik RadWindow but it can be any window object, and you can probably also just store the window handle but using this property permits manipulation of the object more smoothly e.g. HelpWin.BringToFront() as in the above example...
...
...
private Telerik.Windows.Controls.RadWindow **HelpWin**
{
get;
set;
}
...
...
In the window itself (WPF window)
///<summary>
/// Flag to indicate the window is open - use to prevent opening this particular help window multiple times...
///</summary>
public static bool IsOpen { get; private set; }
...
...
...
private void HelpWindowLoaded(object sender, RoutedEventArgs e)
{
IsOpen = true;
}
private void HelpWindowUnloaded(object sender, RoutedEventArgs e)
{
IsOpen = false;
}
and in the view Xaml
...
...
DataContext="{Binding Path=OnlineHelpViewModelStatic,Source={StaticResource Locator}}"
RestoreMinimizedLocation="True"
**Loaded="HelpWindowLoaded" Unloaded="HelpWindowUnloaded"** >
Here's an alternative approach that doesn't require a static property to set and update in each of your window:
public static bool IsWindowInstantiated<T>() where T : Window
{
var windows = Application.Current.Windows.Cast<Window>();
var any = windows.Any(s => s is T);
return any;
}
Usage:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
if (IsWindowInstantiated<SettingsWindow>())
return;
var window = new SettingsWindow();
window.Show();
}
How about using a Singleton?
public class MyWindow : Window {
private static MyWindow instance;
public static MyWindow Instance
{
get
{
if (instance == null)
{
instance = new MyWindow();
}
return instance;
}
}
}
Then just use
MyWindow.Instance.Show() and MyWindow.Instance.Hide()
I found this because I am trying to ensure my users do not open multiple instances of an rtsp stream window. I like Aybe's answer, it works well and is easy to understand.
I have built on it a bit as I wanted to bring the window into focus if it is open.
Here is my code:
public static void OpenWindow<T>() where T: Window
{
var windows = System.Windows.Application.Current.Windows.Cast<Window>();
var any = windows.Any(s => s is T);
if (any)
{
var win = windows.Where(s => s is T).ToList()[0];
if (win.WindowState == WindowState.Minimized)
win.WindowState = WindowState.Normal;
win.Focus();
}
else
{
var win = (Window)Activator.CreateInstance(typeof(T));
win.Show();
}
}
I am also quite new to C# and WPF so I am sure this can be improved even more.
Call it using
OpenWindow<SettingsWindow>();
public static void ShowWindow<T>() where T : Window, new()
{
var existingWindow = Application.Current.Windows.OfType<T>()
.SingleOrDefault();
if (existingWindow == null)
{
new T().Show();
return;
}
existingWindow.WindowState = WindowState.Normal;
existingWindow.Activate();
}
Usage:
ShowWindow<AboutWindow>();
When windows is created then Window.IsLoaded == true. My implementation of singleton windows is:
public partial class MySingletonWindow : Window
{
private static MySingletonWindow _instance = null;
private MySingletonWindow()
{
InitializeComponent();
}
public static MySingletonWindow Show(System.Windows.Window owner = null)
{
// On First call _instance will be null, on subsequent calls _instance will not be null but IsLoaded is false if windows was closed.
if (_instance == null || !_instance.IsLoaded)
_instance = new MySingletonWindow();
_instance.Owner = owner; // Optional owner
_instance.Show(); // Display the window
_instance.Focus(); // Bring it to front
return _instance; // Return instance if user needs it
}
}
Simply show windows using this call:
MySingletonWindow.Show(ParentWindow);
OR
MySingletonWindow.Show();
I'm completely new to GUI programming and need a little help with a list of pictureboxes.
The idea is that I have a list of pictureboxes. When a user clicks on one I want to (for example) change the BorderStyle property of the one selected to be Fixed3D, but change the remaining collection borders to FixedSingle (or something like that). What's the proper way to do something like this? I guess the bigger picture is how do I get a method of one class to call a method of another without having any information about it?
class myPicture
{
private int _pictureNumber;
private PictureBox _box;
public myPicture(int order)
{
_box = new List<PictureBox>();
_box.Click += new System.EventHandler(box_click);
_pictureNumber = order;
}
public void setBorderStyle(BorderStyle bs)
{
_box.BorderStyle = bs;
}
public void box_click(object sender, EventArgs e)
{
//here I'd like to call the set_borders from myPicturesContainer, but I don't know or have any knowledge of the instantiation
}
}
class myPicturesContainer
{
private List<myPicture> _myPictures;
//constructor and other code omitted, not really needed...
public void set_borders(int i)
{
foreach(myPicture mp in _MyPictures)
mp.setBorderStyle(BorderStyle.FixedSingle);
if(i>0 && _MyPictures.Count>=i)
_MyPictures[i].setBorderStyle(BorderStyle.Fixed3d);
}
}
You will need to create a Clicked event in your myPicture class and raise that event when it is clicked. Then you will need to attach to this event in your myPicturesContainer for each instance of myPicture that you have.
Here is a very simple example of what I mean:
class myPicture
{
public event Action<Int32> Clicked = delegate { };
private int _pictureNumber;
public void box_click(object sender, EventArgs e)
{
this.Clicked(this._pictureNumber);
}
}
class myPicturesContainer
{
private List<myPicture> _myPictures;
public void set_borders(int i)
{
foreach (myPicture mp in _myPictures)
{
mp.Clicked += pictureClick;
}
}
void pictureClick(Int32 pictureId)
{
// This method will be called and the pictureId
// of the clicked picture will be passed in
}
}