This is an offline app And I am simply trying to write back to to the Settings.settings file.
In my project under Properties in the file named Settings.settings I add
Name: User, Type: string, Scope:User, Value:Default
After that in my App.xaml.cs file I attempt to read from and write to that value. I have no issue reading from that value, but it seems that it is not even an option to write back.
Here is my code:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
namespace StrainTracker
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
string userName= StrainTracker.Properties.Settings.Default.User;
StrainTracker.Properties.Settings.Default.User = "SomethingSilly"; //line 16
StrainTracker.Properties.Settings.Default.Save(); //line 17
}
}
Here are the errors generated by visual studio.
Error 1 Invalid token '=' in class, struct, or interface member declaration ..\Projects\StrainTracker\StrainTracker\App.xaml.cs 16 63 StrainTracker
Error 2 Invalid token '(' in class, struct, or interface member declaration ..\Projects\StrainTracker\StrainTracker\App.xaml.cs 17 55 StrainTracker
Error 3 'StrainTracker.Properties.Settings.Default' is a 'property' but is used like a 'type' ..\Projects\StrainTracker\StrainTracker\App.xaml.cs 16 43 StrainTracker
Error 4 'StrainTracker.Properties.Settings.Default' is a 'property' but is used like a 'type' ..\Projects\StrainTracker\StrainTracker\App.xaml.cs 17 43 StrainTracker
I can not for the life of me figure out what the issue is. Any thoughts?
Consider placing the code inside a constructor or some method:
public partial class App : Application
{
public App()
{
string userName= StrainTracker.Properties.Settings.Default.User;
StrainTracker.Properties.Settings.Default.User = "SomethingSilly";
StrainTracker.Properties.Settings.Default.Save();
}
}
Related
Resource files are not getting created for the newly added forms when the localized property is set to true in VS 2012.
When I add a new form to the project, set the Localizable property to true and build the application, .resx files are not getting created.
Carefully follow this walkthrough. The experiment I did below in VS 2012 is working fine.
Step1.
Put a Label onto Form1
Set Form1.Localizable = true
Set Form1.Language = Default
Set label's text = "Hello world!"
Step2.
Set Form1.Language = Russian
Set label's text = "Привет мир!"
After these steps resource files become visible in Solution Explorer
Now add following code into Form1's constructor
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
switch (MessageBox.Show(
"Press 'Yes' for default language, 'No' for Russian.",
"Language Option", MessageBoxButtons.YesNo))
{
case System.Windows.Forms.DialogResult.Yes:
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture("");
break;
case System.Windows.Forms.DialogResult.No:
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture("ru");
break;
}
InitializeComponent();
}
}
}
Run the application and see the result.
The main purpose of the code is to show that CurrentUICulture must be set before the method InitializeComponent is called. In real applications, however, setting CurrentUICulture property, usually, takes place on program startup. So the code must be moved to where the program starts.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
switch (MessageBox.Show(
"Press 'Yes' for default language, 'No' for Russian.",
"Language Option", MessageBoxButtons.YesNo))
{
case System.Windows.Forms.DialogResult.Yes:
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture("");
break;
case System.Windows.Forms.DialogResult.No:
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture("ru");
break;
}
Application.Run(new Form1());
}
}
}
If you define UI language setting for your application then you can use the value of the setting here and set UI language. It will affect all forms you have defined in your application.
So I'm trying to work through the Catel 'getting started' examples here:
https://catelproject.atlassian.net/wiki/display/CTL/Getting+started+with+WPF
But I'm getting some errors in visual studio on step 3 (Serializing data from/to disk) - https://catelproject.atlassian.net/wiki/pages/viewpage.action?pageId=15630363
I create a 'top container' model called 'Settings' and a 'child class' of this called 'Global' (pretty much exactly the same as the tutorial except for less properties and different model names).
I create an interface based on the example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using App.Models;
namespace App.Services.Interfaces
{
public interface IGlobalService
{
IEnumerable<Global> LoadGlobals();
void SaveGlobals(IEnumerable<Global> globals);
}
}
Then I create the service implementation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Catel.Collections;
using Catel.Data;
using App.Models;
using App.Services.Interfaces;
namespace App.Services
{
public class GlobalService : IGlobalService
{
private readonly string _path;
public GlobalService()
{
string directory = Catel.IO.Path.GetApplicationDataDirectory("CatenaLogic", "WPF.GettingStarted");
_path = Path.Combine(directory, "global.xml");
}
public IEnumerable<Global> LoadGlobals()
{
if (!File.Exists(_path))
{
return new Global[] { };
}
using (var fileStream = File.Open(_path, FileMode.Open))
{
var settings = Settings.Load(fileStream, SerializationMode.Xml);
return settings.Globals;
}
}
public void SaveGlobals(IEnumerable<Global> globals)
{
var settings = new Settings();
settings.Globals.ReplaceRange(globals);
settings.Save(_path, SerializationMode.Xml);
}
}
}
Visual studio then throws 2 errors and a warning:
Error CS0619 'SavableModelBase.Load(Stream,
SerializationMode)' is obsolete: 'Please use Load(Stream,
SerializationMode, ISerializationConfiguration) instead. Will be
removed in version
5.0.0.'
Error CS0619 'SavableModelBase.Save(string,
SerializationMode)' is obsolete: 'Please use Save(string,
SerializationMode, ISerializationConfiguration) instead. Will be
removed in version 5.0.0.'
Warning CS0618 'CollectionExtensions.ReplaceRange(ObservableCollection,
IEnumerable)' is obsolete: 'Please use ReplaceRange(this
ICollection<T>, IEnumerable<T>) instead. Will be treated as an error
from version 5.0.0. Will be removed in version 5.0.0.'
So far all research I have done on this has come up blank. What is 'ISerializationConfiguration' and how do I implement it? Am I missing something obvious?
Setup is:
Visual Studio 2015 Community (14.0.25425.01 Update 3)
Project targeting .NET 4.5.2
Project initialized using New > Online > WPF application using Catel
NuGet:
Catel.Core 4.5.3
Catel.Extensions.Controls 4.5.3
Catel.MVVM 4.5.3
Catel.Fody 2.14.0
Any help would be much appreciated.
Use the overload as specified by the error / warning:
Load(stream, null);
Save(stream, null);
Environment
Visual Studio 2013 and C++/CLI.
The (ho/e)rror
I faced a situation of IntelliSense giving error on a compiler compliant row. Thus a false positive.
The error is the following:
IntelliSense: function "< full qualified method name >" cannot be
called with the given argument list argument types are: <
expected argument type > object type is: < object type >
What happened
I made a UserControl. There I declared a custom event with relative delegate. I created a form. In form constructor I alloc a my user control instance and try to attach a form method to the control custom event.
Compiler says everything is ok. IntelliSense tells me that event attachment mismatches types.
How to reproduce
I digged in the problem and created an essential context that should reproduce the problem:
Create a solution with two projects: FavaTest (as ClassLibrary) and FavaForm (as Console application...or whatever).
In FavaTest create a UserControl whose name is FavaClass and paste the following in FavaClass.h.
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace FavaTest {
public ref class FavaClass : public System::Windows::Forms::UserControl
{
public:
FavaClass(void)
{
InitializeComponent();
}
// -- here defines very simple event --
delegate void FavaDelegate();
event FavaDelegate^ FavaEvent;
protected:
~FavaClass()
{
if (components)
{
delete components;
}
}
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->SuspendLayout();
//
// FavaClass
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->Name = L"FavaClass";
this->Size = System::Drawing::Size(249, 147);
this->ResumeLayout(false);
}
#pragma endregion
};
}
In project FavaForm create a Form whose name is LaForm and paste the following in LaForm.h
namespace FavaForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class LaForm : public System::Windows::Forms::Form
{
public:
LaForm(void)
{
InitializeComponent();
// here simply allocs a FavaClass object and try to attach to FavaEvent event
FavaTest::FavaClass ^item = gcnew FavaTest::FavaClass();
item->FavaEvent += gcnew FavaTest::FavaClass::FavaDelegate(this, &LaForm::onfava);
}
void onfava(){}
protected:
~LaForm()
{
if (components)
{
delete components;
}
}
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->SuspendLayout();
//
// LaForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(368, 261);
this->Name = L"LaForm";
this->Text = L"LaForm";
this->ResumeLayout(false);
}
#pragma endregion
};
}
Build FavaTest
In FavaForm project common properties add a new reference to FavaTest in order to use its generated dll as a dependencie
Build solution.
Now, while the compiler heralds everything is fine, you should see that IntelliSense complains something on the event attachment row, with the following errore message:
IntelliSense: function "FavaTest::FavaClass::FavaEvent::add" cannot be called with the given argument list
argument types are:
(FavaTest::FavaClass::FavaDelegate ^)
object type is: FavaTest::FavaClass ^
Ready to run package
I packaged all this in a side-test-standalone-solution zip file in order to make it possibile to unzip and run, but unfortunately (IMHO also questionably) I cannot post it here dued to SE guidelines, so it's up to you to make the debug context according to the above.
The question
I could also be missing something, but I used several times this algorithm before and it worked perfectly, now I'm experiencing this on two different machines (VS2013 and VS2015). Does this error apply to you too? And what's wrong with IntelliSense? It is such a simple scenario that I can't imagine I'm the only one experiencing it. I found no clue on the Internet though.
Solved.
It came out that in order to avoid IntelliSense to get crazy, the delegate definition has to be out of the event parent class scope. So in my situation all I had to do to get things right (for IntelliSense) was to move delegate outside the class adding the public keyword, such as:
namespace FavaTest
{
public delegate void FavaDelegate(); // moved out of the class with "public"
public ref class FavaClass : public System::Windows::Forms::UserControl
{
public:
[...]
// -- here defines very simple event --
// delegate row away from here
event FavaDelegate^ FavaEvent;
[...]
}
}
I'm having trouble figuring out the syntax for a JScript .NET dictionary object. I have tried
private var myDictionary: Dictionary<string><string>;
but the compiler complains that it's missing a semicolon and that the Dictionary object is not declared.
I know JScript does have a native dictionary-like object format, but I'm not sure if there are disadvantages to using it instead of the .NET-specific construct. I.e., what if someone wants to extend my script using another .NET language?
JScript.Net doesn't support generic types and methods.
Check this link:
http://msdn.microsoft.com/en-us/library/xfhwa508(v=VS.80).aspx
Click on the JScript Tab under Syntax heading.
There is One way to use the Dictionary Type in JScript.Net
Create a c# assembly
using System;
using System.Collections.Generic;
using System.Text;
namespace Sample
{
class Tools
{
public Dictionary<string,object> Dict()
{
return new Dictionary<string,object>();
}
}
}
IN JScript
import System;
import System.Text;
package Sample
{
public class Main
{
public var MyDictionary = Tools.Dict();
function Main()
{
MyDictionary["test"] = "Works";
Console.WriteLine(MyDictionary["test"]);
}
}
}
FTW
so you can use c# assembly with your JScript.net stuff
How can I read the delay, left and top offset data for each frame of a gif? I've gotten this far.
Load the Gif
var myGif = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
Get a frame
var frame = myGif.Frames[i];
From MSDN: Native Image Format Metadata Queries read (ushort)Metadata.GetQuery("/grctlext/Delay"), (ushort)Metadata.GetQuery("/imgdesc/Left"), (ushort)Metadata.GetQuery("/imgdesc/Top")
But two things don't work. First the Metadata property of both the gif and the frame are always null, even if I try different animated gif files. Second, the Metadata property of the frame doesn't seem to have a GetQuery method.
How do I run these queries, what did I miss?
Edit:
Here is sample code that gives me null metadata. Using a fresh install of VS2010 Premium, on a fresh WPF application. The image file is the one in the comments.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var uri = new Uri(#"c:\b-414328-animated_gif_.gif");
var myGif = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
var frame = myGif.Frames[0];
Title = "";
Title += "Global Metadata is null: " + (myGif.Metadata == null).ToString();
Title += "; Frame Metadata is null: " + (frame.Metadata == null).ToString();
// Crash due to null metadata
//var frameData = (BitmapMetadata)frame.Metadata;
//var rate = (ushort)frameData.GetQuery("/grctlext/Delay");
}
}
}
First, you need to Freeze the Frame you want to obtain the metadata from:
var frame = myGif.Frames[0];
frame.Freeze();
Second, the frame.Metadata returns an ImageMetadata which does not have a GetQuery method, but in fact the object returned is of type BitmapMetadata which has a GetQuery method, so you just need to cast frame.Metadata to BitmapMetadata as you do in the last commented lines of your code.