I developing an wpf application with MVVM Light and I have all my ViewModels and Models within a class library. Now I would like to change a user setting but dont have access to the settings of my UI-Project in the viewmodel of my class library.
Question: Do I have to implement the change of a user setting into my View(UI)-Project to get this to work or ist there a way I could do this in my class library?
the only object I could use there is the Application.Current.Properties but that dont seem to be the correct ones:
Application.Current.Properties["Design"] = s;
I know I have to use:
Settings.Default.Design = s;
and on Exit I need to save all my settings to make the change persistent:
Settings.Default.Save();
I've seen something here - that seems to be a similar issue but I dont understand the answers...
I found an easy solution by myself. Just need to use the OnExit-Event (App.Xaml.cs):
protected override void OnExit(ExitEventArgs e)
{
if (Settings.Default.Design != DesignName())
{
Settings.Default.Design = DesignName();
Settings.Default.Save();
}
}
Related
I just want to start by saying that I've done a lot of research but couldn't find an answer, hence the post.
I'm adding user settings functionality to my app which works as a plugin inside a common off the shelf program for architecture (called Autodesk Revit). The main project (let's call it MainProj) has several dependencies including a project that handles logging and usage (let's call it Loggers). I created a Settings file inside the Loggers project with the goal to have users change the logging level from Error to Debug when there are issues so I can ask them to make the change and send me the log.
The issue I'm facing is that when I change the log level directly inside the config file and re-run the command from within Revit, the change doesn't get translated into the log, as if the log level is somehow compiled during design and is never changed.
I actually tried to reproduce the problem in a simpler way and created a little console program and I'm facing the same issue. Below is the code from the Loggers project.
namespace Loggers
{
public static class Logger
{
public static string RunMe()
{
if (Properties.Settings.Default.LogMode == "Debug") { return "DEBUG"; }
else return "NOTHING";
}
}
}
I then changed the LogMode property from Debug to anything else in the config file but the console kept on returning DEBUG.
static void Main(string[] args)
{
Console.WriteLine(Logger.RunMe());
Console.Read();
}
I also tried changing the setting from user to application and editing its value in the config file and re-running the command but the outcome was the same.
Any help would be very much appreciated. I've been stuck on this for a while. Thank you.
Thanks to #BurnsBA, the link you shared had comments saying that the user.config lives in a different folder and it's not created until the user changes a setting. This made me understand that there wasn't a point in manually editing the app.config and expect the settings to work.
I then did some testing by creating a simple form with a checkbox linked to the Property I wanted to change and the user.config file gets created straight after I call the Save() method on the Properties.
private void btnOK_Click(object sender, EventArgs e)
{
if (chkDebugMode.Checked == true)
Loggers.Properties.Settings.Default.LogMode = "Debug";
else Loggers.Properties.Settings.Default.LogMode = "Error";
Loggers.Properties.Settings.Default.Save();
Close();
}
This is my code:
public class CrazyWindow: EditorWindow
{
[MenuItem("Window/CrazyWindow")]
public static void Window()
{
EditorWindow.GetWindow(typeof(CrazyWindow));
Debug.Log("It should have appeared!");
}
string test = "";
public void OnGUI()
{
test = EditorGUILayout.TextField ("Text Field", test );
}
}
I'm using Unity3D v. 4.3.4f1 (free version) on Windows 7.
I have no idea why this is happening, as I can see in tutorials in the internet, that's how it should be done. The script is also in the Editor folder.
I'm able to click on the option "CrazyWindow" in the window menu, and I also get the Debug message informing me that the window should be working, but nothing happens besides that. No window is created at all!
What might be the cause of my problem?
Problem solved.
As Bart mentioned, I was using a custom Editor Layout, which was the case for the window not showing.
I just switched to one of the factory editor layouts and: ta dah, the window was there...
Pretty buggy thought.
Try renaming the 'CrazyWindow' part in the MenuItem and of the class itself. Unity remembers whether a window is visible or not and somehow something goes wrong there. Probably it thinks your window is visible (in cache) while actually it is not.
As Bart said, it remembers useless things
Just make it remember what we want it to
private void OnLostFocus() {
GetWindow<CrazyWindow>().Close();
}
Heey,
I'm currently working on my second XNA/Monogame game for Windows 8/Metro but ran into a problem. We now came at the point which we need to store a highscore with a name attached to it so I need to handle the onscreen keyboard to get the info.
I searched through the forum and I found some topics related to this but no post with some example code or a description which helped me completely fixing my problem. I changed my project to a XAML template and I got a TextBox working in my GamePage but now I need to get the TextBox inside my game loop to read it out so I can save the name besides my score and I have currently no idea how to do this.
My current code of my GamePage.cs
public GamePage(string launchArguments)
{
this.InitializeComponent();
// Create the game.
_game = XamlGame<Main>.Create(launchArguments, Window.Current.CoreWindow, this);
txtTest.TextChanged += txtTest_TextChanged;
}
void txtTest_TextChanged(object sender, TextChangedEventArgs e)
{
Debug.WriteLine(txtTest.Text); //Write content to public string in Main.cs
}
I found out how I can write the content of the TextBox to a string inside my gameloop but now I'm stuck how I can control the TextBox his properties from inside my gameloop so I can set the Visibility and Focus. Do I need to create my own EventHandler which will watch if I set a Boolean or something?
Thanks in advance.
Greetings,
ForT3X
Disclaimer: Let me just say that I've never worked with Windows 8 XAML projects or the GamePage class before but after doing a little googling I think I understand enough to help.
It seems that your issue boils down to a circular dependency. You want 2-way communication between your GamePage and your Game class.
Communicating from the GamePage to the Game class is easy, because the GamePage is already responsible for creating the Game class and storing it in the _game member variable. Therefore, to send messages from your GamePage to the Game you just need to add a method to your Game class, for example:
void txtTest_TextChanged(object sender, TextChangedEventArgs e)
{
_game.SetHighscoreName(txtTest.Text);
Debug.WriteLine(txtTest.Text); //Write content to public string in Main.cs
}
Communicating back the other way (from Game to GamePage) is a little trickier, but it can be solved using an interface and property injection.
First, create an interface that belongs to your Game class. What I mean by that is; it lives in the same project and or namespace as the Game class. It might look something like this:
public interface IGamePageController
{
void ShowHighscoreTextBox();
}
Then, add a property to your Game class like this:
public IGamePageController GamePageController { get; set; }
Next, have the GamePage class implement the interface like so:
public partial class GamePage : PhoneApplicationPage, IGamePageController
{
//...
public void ShowHighscoreTextBox()
{
txtTest.Visibility = Visibility.Visible;
}
}
And finally, in the GamePage constructor you need to set the GamePageController property.
// Create the game.
_game = XamlGame<Main>.Create(launchArguments, Window.Current.CoreWindow, this);
_game.GamePageController = this;
Once you have this pattern in place, it's easy to add new ways for your Game and GamePage classes to communicate by adding more methods to the interface or Game class.
You can share a view model between the XAML page and your game.
The XAML page GamePage creates the instance of your game class. When it does you can also let the game class know about your view model.
_game = XamlGame<Game1>.Create(launchArguments, Window.Current.CoreWindow, this);
_game.XamlGameDataViewModel = new GameDataViewModel();
DataContext = _game.XamlGameDataViewModel;
There is more detail in my post Sharing your view model between Monogame and Xaml
If you need to store some values you should try with IsolatedStorage.
As MSDN says:
"Isolated storage is not available for Windows Store apps. Instead, use the application data classes in the Windows.Storage namespaces included in the Windows Runtime API to store local data and files."
You can find more information here.
Using Windows.Storage you should do something like this:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (string.IsNullOrEmpty((string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["highscore"]))
{
localSettings.Values["highscore"] = highscore;
}
I'm working on a modularised Prism application. After some localized WPF clients with resx files (localized.resx, localized.de.resx etc.), i thought i could adapt this technique to my prism modules. But the module always takes the default resx.
Any suggestions?
Thanks in advance
Sorry but it's not easy to explain this with a short message. So I wrote a really simple prism project with a working solution.
Try to download this example
I found out, that my ResourceManager has the wrong values in it's ResourceSet for the specific language. See my New Topic
Actually its pretty easy, just make sure to force your localization inside CreateShell() inside app.xaml.cs like this:
protected override Window CreateShell()
{
SetLocalization();
return Container.Resolve<MainWindow>();
}
the SetLocalization() method can be like this :
private void LoadLocalizationFromSettings()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");
//set pc default localization to be used in the XAML gui
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata("en-us")));
}
I've been looking at modifying the source of the Doppler podcast aggregator with the goal of being able to run the program directly from my mp3 player.
Doppler stores application settings using a Visual Studio designer generated Settings class, which by default serializes user settings to the user's home directory. I'd like to change this so that all settings would be stored in the same directory as the exe.
It seems that this would be possible by creating a custom provider class which inherits the SettingsProvider class. Has anyone created such a provider and would like to share code?
Update: I was able to get a custom settings provider nearly working by using this MSDN sample, i.e. with simple inheritance. I was initially confused as Windows Forms designer stopped working until I did this trick suggested at Codeproject:
internal sealed partial class Settings
{
private MySettingsProvider settingsprovider = new MySettingsProvider();
public Settings()
{
foreach (SettingsProperty property in this.Properties)
{
property.Provider = settingsprovider;
}
...
The program still starts with window size 0;0 though.
Anyone with any insight to this?
Why the need to assing the provider in runtime---instead of using attributes as suggested by MSDN?
Why the changes in how the default settings are passed to the application with the default settings provider vs. the custom one?
Why not use the CodeProject PortableSettingsProvider solution as is (with a few minor changes) ?
I have done so in my project (StreamRecorder.NET) with success.
Some comments on the project's page were useful:
http://www.codeproject.com/Messages/2934144/Fixed-csharp-version.aspx
http://www.codeproject.com/Messages/3285411/Re-Win-Form-Designer-breaking-with-custom-Settings.aspx
And the code I ended up with:
static void Main(string[] args)
{
if (args.Contains("-p") || args.Contains("--portable"))
{
MakePortable(Properties.Settings.Default);
MakePortable(Properties.LastUsedSettings.Default);
MakePortable(Properties.DefaultSettings.Default);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm(args));
}
private static void MakePortable(ApplicationSettingsBase settings)
{
var portableSettingsProvider =
new PortableSettingsProvider(settings.GetType().Name + ".settings");
settings.Providers.Add(portableSettingsProvider);
foreach (System.Configuration.SettingsProperty prop in settings.Properties)
prop.Provider = portableSettingsProvider;
settings.Reload();
}
Lastly I made these changes to the CP project:
string _fileName;
public PortableSettingsProvider(string fileName)
{
_fileName = fileName;
}
public virtual string GetAppSettingsFilename()
{
//Used to determine the filename to store the settings
//return ApplicationName + ".settings";
return _fileName;
}
I know this question is quite old already. I just want to share my own version of a portable settings provider which I published as nuget package here.
The usage is pretty simple:
// make the default settings class portable
PortableSettingsProvider.ApplyProvider(Properties.Settings.Default);
I also explained the basic strategy of this implementation at https://www.codeproject.com/Articles/1238550/Making-Application-Settings-Portable.
Just to 'close' the question: The somewhat unsatisfactory solution I ended up with was
Create a custom settings provider, which inherits from SettingsProvider and stores the settings in a XML file
Set the Provider property of each of the setting (by selecting the entire grid in the designer) to the custom settings provider using the designer
Drawbacks: The forms designer breaks and gives an exception which basically says that the custom provider class cannot be found. The built exe however works OK. Setting the provider in the code as described in the question makes the designer work, but then for some reason, which I haven't looked closely at, the settings won't serialize.
It seems that making settings portable was all that was needed to make Doppler portable. Whether I'll start using Doppler as my main podcast aggregator or stick with my homebrew command line aggregator, I'll see.