C
C#2d ago
wildREA

How to Correctly Reference a Method in App.xaml.cs

Hey, reviewer. The ChangeLanguage method should work, but referencing is a challenge at the moment, since I get the error 'App' does not contain a definition for 'ChangeLanguage' and no accessible extension method 'ChangeLanguage' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?). The OnLanguageChanged method works until I add that reference for ((App)Application.Current).... How do I correctly reference to a method in App.xaml.cs? ---- Settings.xaml.cs
public partial class Settings : UserControl
{
public Settings()
{
InitializeComponent();
}

private void OnLanguageChanged(object sender, SelectionChangedEventArgs e)
{
string? selectedLanguage = ((ComboBoxItem?)e.AddedItems[0]).Tag.ToString();
Debug.WriteLine(selectedLanguage);
((App)Application.Current).ChangeLanguage(selectedLanguage);
}
}
}
public partial class Settings : UserControl
{
public Settings()
{
InitializeComponent();
}

private void OnLanguageChanged(object sender, SelectionChangedEventArgs e)
{
string? selectedLanguage = ((ComboBoxItem?)e.AddedItems[0]).Tag.ToString();
Debug.WriteLine(selectedLanguage);
((App)Application.Current).ChangeLanguage(selectedLanguage);
}
}
}
App.xaml.cs
public partial class App : Application
{
public void ChangeLanguage(string language)
{
public partial class App : Application
{
public void ChangeLanguage(string language)
{
4 Replies
Nasdack
Nasdack2d ago
You can use dependency injection or make the method static if it does not access instance data
wildREA
wildREAOP2d ago
I have this and ServiceProvider is apparently not defined in App despite having defined it. I've used using Microsoft.Extensions.DependencyInjection;. Settings.xaml.cs
// Access ChangeLanguage method via DI
var languageService = (IAppLanguageServices)App.ServiceProvider.GetService(typeof(IAppLanguageServices));
languageService?.ChangeLanguage(selectedLanguage);
// Access ChangeLanguage method via DI
var languageService = (IAppLanguageServices)App.ServiceProvider.GetService(typeof(IAppLanguageServices));
languageService?.ChangeLanguage(selectedLanguage);
App.xaml.cs
// Static ServiceProvider
public static IServiceProvider ServiceProvider { get; private set; }

protected override void OnStartup(StartupEventArgs e)
{
var serviceCollection = new ServiceCollection();

// Registering App class as IAppLanguageServices
serviceCollection.AddSingleton<IAppLanguageServices>(this);

// Build the service provider
ServiceProvider = serviceCollection.BuildServiceProvider();

base.OnStartup(e);
}
// Static ServiceProvider
public static IServiceProvider ServiceProvider { get; private set; }

protected override void OnStartup(StartupEventArgs e)
{
var serviceCollection = new ServiceCollection();

// Registering App class as IAppLanguageServices
serviceCollection.AddSingleton<IAppLanguageServices>(this);

// Build the service provider
ServiceProvider = serviceCollection.BuildServiceProvider();

base.OnStartup(e);
}
'App' does not contain a definition for 'ServiceProvider'
Nasdack
Nasdack2d ago
Check that you're referencing the correct App. ServiceProvider is static and public and it must be there. Although the best practice for a service locator class is for it to be encapsulated So create a static method that fetches the service Don't expose the actual ServiceProvider
wildREA
wildREAOP2d ago
@Nasdack I'll try it on Monday at 8.

Did you find this page helpful?