Shock
Shock
CC#
Created by Shock on 11/21/2024 in #help
Dependency Injection returning null value
I've created a WPF application that uses DI for the View/Viewmodel. I'm attempting to create a CS file to deal with the program logic that uses a dependency injected Service Wrapper. However, whenever I call the Service in the new class, the value is returning as null. I've included a shortened version below that hopefully gets the point across.
MyService.CS
public class CustomWrapper : ICustomWrapper
{
public Connect () {}
public Disconnect () {}
}
public interface ICustomWrapper
{
public Connect(string);
public Disconnect();
}
public class CustomWrapper : ICustomWrapper
{
public Connect () {}
public Disconnect () {}
}
public interface ICustomWrapper
{
public Connect(string);
public Disconnect();
}
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public static string radioParse {get; set;}
private readonly ICustomWrapper _customWrapper;
public MainWindow (ICustomWrapper customWrapper)
{
_customWrapper = customWrapper;
InitializeComponent();
}

public void Proceed_Click(object sender, RoutedEventArgs e)
{
var radioButton = Session.Children.OfType<RadioButton>()
.FirstOrDefault(r=> r.IsChecked.HasValue && r.IsChecked.Value);
if (radioButton == null)
MessageBox.Show("Select an Option");
else if (radioButton != null)
{
radioParse = "A"
SelectValue();
}
}
public void SelectValue();
{
_customWrapper.Connect(radioParse)
}
public partial class MainWindow : Window
{
public static string radioParse {get; set;}
private readonly ICustomWrapper _customWrapper;
public MainWindow (ICustomWrapper customWrapper)
{
_customWrapper = customWrapper;
InitializeComponent();
}

public void Proceed_Click(object sender, RoutedEventArgs e)
{
var radioButton = Session.Children.OfType<RadioButton>()
.FirstOrDefault(r=> r.IsChecked.HasValue && r.IsChecked.Value);
if (radioButton == null)
MessageBox.Show("Select an Option");
else if (radioButton != null)
{
radioParse = "A"
SelectValue();
}
}
public void SelectValue();
{
_customWrapper.Connect(radioParse)
}
MainWindow.Xaml
<StackPanel>
<Button Content = "Proceed"
Command = "{Binding Path=NavigateHomeCommand}
Click = "Proceed_Click"/>
</StackPanel>
<StackPanel>
<Button Content = "Proceed"
Command = "{Binding Path=NavigateHomeCommand}
Click = "Proceed_Click"/>
</StackPanel>
MainViewModel.cs
private ICustomWrapper _customWrapper;
public RelayCommand NavigateHomeCommand
public MainViewModel(INavigationService navigationService, ICustomWrapper customWrapper)
{
NavigateHomeCommand = new RelayCommand (o=> {Navigation.NavigateTo<HomeViewModel>(); } o=> true;
}
private ICustomWrapper _customWrapper;
public RelayCommand NavigateHomeCommand
public MainViewModel(INavigationService navigationService, ICustomWrapper customWrapper)
{
NavigateHomeCommand = new RelayCommand (o=> {Navigation.NavigateTo<HomeViewModel>(); } o=> true;
}
App.xaml
<DataTemplate DataType = "{x:Type TypeName=viewModel:HomeViewModel}">
<view:HomeView/>
</DataTemplate>
<DataTemplate DataType = "{x:Type TypeName=viewModel:HomeViewModel}">
<view:HomeView/>
</DataTemplate>
App.Xaml.Cs
public partial class App : Application
{
private readonly IServiceProvider _serviceProvider;
private readonly IService Collection services = new ServiceCollection();
{
public App
{
services = new ServiceCollection();
services.AddSingleton<MainWindow>(s =>
{
var window = new mainWindow(s.GetRequiredService<ICustomWrapper>());
window.DataContext = s.GetRequiredService<MainViewModel>();
return window;
});
services.AddSingleton<MainViewModel>();
services.AddSingleton<HomeViewModel>();
services.AddSingleTon<INavigationService, navigationService>();
services.AddSingleTon<ICustomWrapper, customWrapper>();
services.AddSingleton<Func<Type, ViewModelBase>>(serviceProvider => viewModelType => (ViewModelBase)serviceProvider.GetRequiredService(viewModelType));
_serviceProvider = services.BuildServiceProvider();
}

protected override void ONstartup(StartupEventArgs e)
{
MainWindow = _serviceProvider.GetRequiredService<MainWindow>();
MainWindow.Show();
base.OnStartup(e);
}
public partial class App : Application
{
private readonly IServiceProvider _serviceProvider;
private readonly IService Collection services = new ServiceCollection();
{
public App
{
services = new ServiceCollection();
services.AddSingleton<MainWindow>(s =>
{
var window = new mainWindow(s.GetRequiredService<ICustomWrapper>());
window.DataContext = s.GetRequiredService<MainViewModel>();
return window;
});
services.AddSingleton<MainViewModel>();
services.AddSingleton<HomeViewModel>();
services.AddSingleTon<INavigationService, navigationService>();
services.AddSingleTon<ICustomWrapper, customWrapper>();
services.AddSingleton<Func<Type, ViewModelBase>>(serviceProvider => viewModelType => (ViewModelBase)serviceProvider.GetRequiredService(viewModelType));
_serviceProvider = services.BuildServiceProvider();
}

protected override void ONstartup(StartupEventArgs e)
{
MainWindow = _serviceProvider.GetRequiredService<MainWindow>();
MainWindow.Show();
base.OnStartup(e);
}
There is a lot more there, but they are all instantiated as singleton. HomeView.xaml.Cs
public partial HomeView : UserControl
{
private readonly ICustomWrapper _customWrapper;
private readonly MainWindow mainWindow;

public HomeView(ICustomWrapper customWrapper, MainWindow mainWindow)
{
_mainWindow = mainWindow
customWrapper = customWrapper
}
private HomeView()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
Logic _logic = new Logic(_customWrapper, _mainWindow);
_logic.Select();
}
}
public partial HomeView : UserControl
{
private readonly ICustomWrapper _customWrapper;
private readonly MainWindow mainWindow;

public HomeView(ICustomWrapper customWrapper, MainWindow mainWindow)
{
_mainWindow = mainWindow
customWrapper = customWrapper
}
private HomeView()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
Logic _logic = new Logic(_customWrapper, _mainWindow);
_logic.Select();
}
}
And finally. Logic.CS
public class Logic
{
private readonly ICustomWrapper _customWrapper;
private readonly MainWindow _mainWindow
public string connectLetter = Mainwindow.radioParse;

public Logic(ICustomWrapper customWrapper, MainWindow mainWindow)
{
_MainWindow = mainWindow;
_customWrapper = customWrapper;
}
public void Select()
{
_customWrapper.Disconnect();
]
public class Logic
{
private readonly ICustomWrapper _customWrapper;
private readonly MainWindow _mainWindow
public string connectLetter = Mainwindow.radioParse;

public Logic(ICustomWrapper customWrapper, MainWindow mainWindow)
{
_MainWindow = mainWindow;
_customWrapper = customWrapper;
}
public void Select()
{
_customWrapper.Disconnect();
]
21 replies
CC#
Created by Shock on 11/13/2024 in #help
WPF local: when using Dependency Injection
Hello, My WPF app is utilizing dependency injection following https://achraf-chennan.medium.com/net-core-dependency-injection-for-wpf-core-62dc282690f8 (which removes StartupUri from the App.xaml file). When I'm attemping to utilize data binding, but local:SessionConverter isn't being recognize and I'm not understanding how to work around it as I'm unable to set the DataContext in the main window due to the parameters.
public enum Data
{
Data1,
Data2,
Data3
}
public partial class MainWindow : Window
{
private readonly ICustomWrapper _customWrapper;
public MainWindow(ICustomWrapper customWrapper)
{
InitializeComponent();
}
private Data _data;
public Data data
{
get=> _data;
set
{
_data = value;
MessageBox.Show("The selected datum is: " + "value.ToString());
}

public class SessionConverter : IValueConverter
{
public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.Equals(true) == true ? parameter : Binding.DoNothing;
}
}
public enum Data
{
Data1,
Data2,
Data3
}
public partial class MainWindow : Window
{
private readonly ICustomWrapper _customWrapper;
public MainWindow(ICustomWrapper customWrapper)
{
InitializeComponent();
}
private Data _data;
public Data data
{
get=> _data;
set
{
_data = value;
MessageBox.Show("The selected datum is: " + "value.ToString());
}

public class SessionConverter : IValueConverter
{
public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.Equals(true) == true ? parameter : Binding.DoNothing;
}
}
The .xaml code looks like the following (button data removed)
<Window x:Class="DataTest.MainWindow"
xmlns:local="clr-namespace:DataTest"/>
<Window.Resources>
<local:SessionConverter x:Key="SessionConverter"/>
</Window.Resources>
<Grid>
<StackPanel>
<RadioButton>
</RadioButton>
</StackPanel>
</Grid>
<Window x:Class="DataTest.MainWindow"
xmlns:local="clr-namespace:DataTest"/>
<Window.Resources>
<local:SessionConverter x:Key="SessionConverter"/>
</Window.Resources>
<Grid>
<StackPanel>
<RadioButton>
</RadioButton>
</StackPanel>
</Grid>
Could anyone point me in the right direction here?
14 replies
CC#
Created by Shock on 11/7/2024 in #help
Out Parameter Question
I think this should be simple, but I am coming up blank on a method call with an out parameter. (The example below is basic but my actual code requires the parameters) File1.Cs
class Example
{
public void Position(int row, int col, out string txt)
{
StringBuilder Data = new StringBuilder(20);
UInt64 f = (UInt64)(row * col);
txt=Data.ToString();
return f;
}
}
class Example
{
public void Position(int row, int col, out string txt)
{
StringBuilder Data = new StringBuilder(20);
UInt64 f = (UInt64)(row * col);
txt=Data.ToString();
return f;
}
}
If I am trying to call the Position Method in File2.Cs
namespace Example
{
class Example2
{
public void Call()
{
Example p = new Example();
p.Position(1,2,???);
namespace Example
{
class Example2
{
public void Call()
{
Example p = new Example();
p.Position(1,2,???);
What do I put for the last parameter on p.Position?
16 replies