Cannot share a view into mutiple component - c#

I got a task that need to share camera view from a webcam into 2 Window. I've tried using singleton for create camera view only one time. The problem is that created instance can not share between 2 class. I'm now really confused. What am I doing wrong here?
MainWindow class
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ReceptionMainWindow receptionMainWindow = new ReceptionMainWindow();
CustomerMainWindow customerMainWindow = new CustomerMainWindow();
receptionMainWindow.Show();
customerMainWindow.Show();
this.Hide();
}
}
Receptionist class
public partial class ReceptionMainWindow : Window
{
public ReceptionMainWindow()
{
InitializeComponent();
ReceptionWindowHoler.Content = CameraPage.getInstance();
}
}
Customer Class
public partial class CustomerMainWindow : Window
{
public CustomerMainWindow()
{
InitializeComponent();
CustomerWindowHoler.Content = CameraPage.getInstance();
}
}
private static CameraPage instance;
Camera class
public CameraPage()
{
InitializeComponent();
//DataContext = CameraViewModel.getInstance();
this.DataContext = this;
GetVideoDevices();
}
public static CameraPage getInstance()
{
if (instance == null)
instance = new CameraPage();
return instance;
}

You could create a Bitmap from the camera view and share it via events to the two other windows.
Take this answer as a workaround. There must be a better way.

Related

C#: Change WindowState of main window from a different class

I have one the most default class of my window and second class I call from it. I need to change the WindowState of a MainWindow window from that second class but when I try to do it by: MainWindow.WindowState = WindowState.Normal; it gives me this error: An object reference is required for the non-static field, method, of property 'Window.WindowState'.
MainWindow.xaml.cs:
namespace Client_Patcher
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
UnminimizeApplicationCommand.cs:
namespace Client_Patcher
{
public class UnminimizeApplicationCommand : ICommand
{
public UnminimizeApplicationCommand(WindowState d)
{
}
public bool CanExecute(object parameter)
{
throw new NotImplementedException();
}
public void Execute(object parameter)
{
//Need to change WindowState property of a MainWindow here.
}
public event EventHandler CanExecuteChanged;
event EventHandler ICommand.CanExecuteChanged
{
add
{
throw new NotImplementedException();
}
remove
{
throw new NotImplementedException();
}
}
}
}
Also, I tried to do this:
namespace Client_Patcher
{
public partial class MainWindow : Window
{
public MainWindow AppWindow { get; }
public MainWindow()
{
InitializeComponent();
AppWindow = this;
}
}
}
Which is called by MainWindow.AppWindow.WindowState = WindowState.Normal; way, but gives the same error. What is the work aroud?
This error occurs because you are treating MainWindow like a static object. But in fact it isn't. With this in mind and now when reading the error message again, you will realize that it informs you exactly about this issue. To solve this try
Application.Current.MainWindow.WindowState = WindowState.Normal;
inside your Execute callback
I recommend using something called an "IoC container"
Create a static class.
public static IoC
{
private static Window _window;
// The main window instance for your app
public static Window Window => _window;
// Here you pass your application's "main" window instance.
// So all window related intractability happens only from this clas
public static InitializeDI(Window window)
{
_window = window;
}
}
Note:
You should really call this method in the Main() method where your application starts

WPF- How can i use a List that i declare and fill in my MainWindow in a Usercontrol?

Hi i need to be able to use a list from my Main Window in a Usercontrol i need to be able to edit and read from it in various Usercontrols.
MainWindow:
public partial class MainWindow : Window
{
public List<Termin> termine = new List<Termin>();
public MainWindow()
{
InitializeComponent();
}
}
Usercontrol:
public partial class KalenderAnsicht : UserControl
{
public KalenderAnsicht()
{
InitializeComponent();
}
private void SomeMethod()
{
//i need to be able to use the list here
}
}
You need to get a reference to the MainWindow one way or another. The easiest way to do this is probably to use the Application.Current.Windows property:
private void SomeMethod()
{
var mw = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
List<Termin> termine = mw.termine;
//...
}
You could also consider making termine static:
public partial class MainWindow : Window
{
public static List<Termin> termine = new List<Termin>();
public MainWindow()
{
InitializeComponent();
}
}
...and access it directly without a reference to an instance of MainWindow:
private void SomeMethod()
{
List<Termin> termine = MainWindow.termine;
//...
}

Access a BindingSource in one class from another class

I have 2 classes in the same project, ProjectView and FeatureView. I need to access a BindingSource in one class from another class. I have a kluge in which I make the BindingSource scope internal instead of private. Shame, shame. Is there a better way to do this?.
// ProjectView.cs
public partial class ProjectView : System.Windows.Forms.UserControl {
}
// ProjectView.Designer.cs
partial class ProjectView {
// This should be private
internal System.Windows.Forms.BindingSource bsFeatures;
}
// FeatureView.cs
public partial class FeatureView : System.Windows.Forms.UserControl {
// Get ProjectView
Project currentProject = this._presenter.WorkItem.State["CurrentProject"] as Infrastructure.Interface.Aml.BusinessEntities.Project;
string key = System.String.Concat("Project", currentProject.Id);
this._presenter.WorkItem.State["CurrentProject"] = currentProject;
ProjectView view = _presenter.WorkItem.Items.Get<ProjectView>(key);
// Populate currentProject.Features with ProjectView.bsFeatures.List
currentProject.Features.Clear();
IList featureList = view.bsFeatures.List;
foreach (Feature feature in featureList)
{
currentProject.Features.Add(feature);
}
}
maybe something like that, not sure:
partial class ProjectView
{
// This should be private
private System.Windows.Forms.BindingSource bsFeatures;
public System.Windows.Forms.BindingSource BindingSource
{
get { return bsFeatures; }
}
public void ShareOnlyWith(FeatureView fw)
{
fw.BindingSource = bsFeatures;
}
}
of course we break one of the principles, don't depend on concretions.

winforms, loop through components

I am trying to loop through components on a form, but components seems to be null.
So, how do I loop through components (NOT CONTROLS) on a form ?
public partial class FormBase : Form
{
public FormBase()
{
InitializeComponent();
FixVisualDesignerIssues();
}
protected void FixVisualDesignerIssues()
{
// this.components is always NULL ????????
foreach (var comp in this.components.Components.OfType<BindingSource>())
{
((BindingSource)comp).do something, whatever
}
}
EDITED as requested
public partial class FormBaseList : Test_app.FormBase
{
public FormBaseList()
{
InitializeComponent();
}
public partial class FormBaseDetail : Test_app.FormBase
{
public FormBaseDetail():base()
{
InitializeComponent();
gttDataGridView1.AutoGenerateColumns = false;
}
If you are creating the BindingSource the following way, only then it will be contained in the components container.
BindingSource bindingSource1 = new BindingSource(components);
If you are using any other way to create the binding source, there will be nothing in the components container.

Access class from another form

I'm struggling to get a class from a different form without making it static, here's what I want to do:
//First form
public partial class SetupScreen : Form
{
Control myObject;
public Battleship myBattleship;
public SetupScreen()
{
InitializeComponent();
//Create Class Object
myBattleship = new Battleship();
}
}
//Launch second form
public partial class GameScreen : Form
{
Control myObject;
Battleship myBattleship;
Battleship fredBattleship;
public GameScreen()
{
InitializeComponent();
//Get the class
myBattleship = SetupScreen.myBattleship;
}
}
I keep getting the error "an object reference is required for the non-static field, method or property"
I want the class to be accessible by the whole form, not just a single method therefore I don't want to pass it through each time because this is a hassle
I don't want to make the class static since it cannot be erased, how would I go about doing this?
You are getting this error because you are trying to access a non-static field in a static manner.
Where do you instantiate SetupScreen and GameScreen?
Why not something like this:
public partial class SetupScreen : Form
{
private Control myObject;
public Battleship myBattleship;
private GameScreen gameScreen;
public SetupScreen()
{
InitializeComponent();
//Create Class Object
myBattleship = new Battleship();
gameScreen = new GameScreen(this);
}
}
public partial class GameScreen : Form
{
private Control myObject;
private Battleship myBattleship;
private Battleship fredBattleship;
private SetupScreen setupScreen;
public GameScreen(SetupScreen setupScreen)
{
InitializeComponent();
this.setupScreen = setupScreen;
myBattleship = this.setupScreen.myBattleship;
}
}
Of course, this will only work if you can instantiate GameScreen in SetupScreen. I could give you a better answer if you tell me where/how you are "launching" these forms.
You could pass a reference of your first form to your second form, or (what I would do), create a public Battleship property on your second form and pass your object that way.
//First form
public partial class SetupScreen : Form
{
Control myObject;
public Battleship myBattleship;
public SetupScreen()
{
InitializeComponent();
//Create Class Object
myBattleship = new Battleship();
Form gameForm = new GameScreen(); // New form object
gameForm.MyBattleship = myBattleship; // Set property
gameForm.Show(); // Show form
}
}
//Second form
public partial class GameScreen : Form
{
Control myObject;
Battleship fredBattleship;
public BattleShip MyBattleship { set; get; }
public GameScreen()
{
InitializeComponent();
}
}
you must use singleton pattern.
so your code must be like this:
//First form
public partial class SetupScreen : Form
{
public static SetupScreen setupScreenFrm;
Control myObject;
public Battleship myBattleship;
public SetupScreen()
{
setupScreenFrm=this;
InitializeComponent();
//Create Class Object
myBattleship = new Battleship();
}
}
//Launch second form
public partial class GameScreen : Form
{
Control myObject;
Battleship myBattleship;
Battleship fredBattleship;
public GameScreen()
{
InitializeComponent();
//Get the class
SetupScreen ssFrm=SetupScreen.setupScreenFrm;
myBattleship = ssFrm.myBattleship;
}
}
and first of all, in the start of your app, create an instant of SetupScreen form.
now you can access to SetupScreen in anywhere.

Categories

Resources