I am currently using UWP Toolkit to navigate among app pages. There is a page that is being used for initializing and opening RaspberryPi GPIO pins. The following error occurs after navigating away from that page then trying to navigate back to it again.
The process cannot access the file because it is being used by another process .\r\n\r\nPin ' is currently opened in an incompatible sharing mode. Make sure this pin is not already in use by this application or another application
I can see that the constructor is being called each time the page is visited and hence there is an attempt to open pins that are already opened. What is the best way to overcome this issue?
You may add NavigationCacheMode = NavigationCacheMode.Required; to the ctor of the page so your app will not create a new instance of it when you navigate there.
What I always do is let a class deal with managing pins so that your user code can request pins to act on.
public class IO
{
private readonly GpioController _gpioController;
private readonly Dictionary<int, GpioPin> _pins;
public IO(GpioController gpioController)
{
_gpioController = gpioController;
_pins = new Dictionary<int, GpioPin>();
}
public GpioPin OpenPin(int pin, GpioSharingMode mode)
{
if (_pins.ContainsKey(pin))
{
var gpioPin = _pins[pin];
if (gpioPin.SharingMode == mode)
{
return gpioPin;
}
throw new ArgumentException($"Pin '{pin}' is already configured in mode '{gpioPin.SharingMode}'");
}
else
{
var gpioPin = _gpioController?.OpenPin(pin, mode);
_pins[pin] = gpioPin;
return gpioPin;
}
}
}
Then my viewmodels simply request a pin as follows
public MainViewModel()
{
_io = ServiceContainer.Instance.Get<IO>();
_brakingPin = _io.OpenPin(4, GpioSharingMode.Exclusive);
_io.SetDriveMode(_brakingPin, GpioPinDriveMode.Output);
_io.Write(_brakingPin, GpioPinValue.Low);
}
Related
I am trying to create C# based Discord bot with pagination but I can't find any way to store the current page so I know what page to select next.
I tried
public class DiscordModule : InteractionModuleBase<SocketInteractionContext>
{
private int page = 0;
[SlashCommand("ping", "Recieve a pong")]
public async Task Ping()
{
var embed = new EmbedBuilder()
{
Title = "Component"
};
var comp = new ComponentBuilder();
comp.WithButton("Increment", "Increment");
await RespondAsync(embed: embed.Build(), components: comp.Build());
}
[ComponentInteraction("Increment")]
public async Task Increment()
{
page = page + 1;
await RespondAsync($"{page}");
}
}
It was just a proof of concept, but I couldn't get it to work as it always returned the same value. I didn't think it would but I saw the following:
Discord.InteractivityAddon That uses a current page variable.
Discord.Addons.Interactive That uses a variable page.
I don't quite get how they achieve a variable tied to the instance of the message sent. I considered using a map message Id to page number but this could end up using a lot of memory.
I know I could just use the libraries linked but I want to implement it myself.
I'm making an educational game (Windows 10 UWP, C# + XAML) and I need to store user information (in particular, their current score) and retrieve it when they start the app again. I've found a way to do this (see code below) but I have no idea if this is a normal solution to this problem. I'm currently creating a txt file and storing and retrieving data in/from it. Are there more common, or simpler ways to do this?
Here's what I'm currently doing:
Create the file:
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.CreateFileAsync("nameOfTextFile.txt", CreationCollisionOption.OpenIfExists); //other options are ReplaceExisting
Open the file:
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync("nameOfTextFile.txt");
Write text to the file:
await FileIO.WriteTextAsync(sampleFile, "Put the added text here");
Read text from the file:
string someVariableName = await FileIO.ReadTextAsync(sampleFile);
-Thanks in advance for any help!!
While the file-based approach is valid, there are easier ways, at least for simple data: You can use roaming (or local) settings. Roaming settings are roamed between devices, as long as their size don't exceed 64K, and would carry the score from the user's desktop to the user's phone, for example. Local settings stay on the machine.
Settings are easy to use:
IPropertySet propertySet = ApplicationData.Current.RoamingSettings.Values;
// Get previous score (or 0 if none)
int score = (int)(propertySet["Score"] ?? 0);
// ...play game...
// Set updated score:
propertySet["Score"] = score;
The way I go about doing projects and settings like this is creating a propery setting in Visual Studio, then Setting and Getting the setting / Value.
You can access this by going to the application properties.
This allows access to read,write, and save information / onload restore information.
Some Informational Links:
https://msdn.microsoft.com/en-us/library/bb397755(v=vs.110).aspx
and (Suggested)
https://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx
OK, so here goes an example of using a class to store your settings in.
There are many, many more ways you could do this. Too many to list.
Create a settings class:
public class YourSettingsClass
{
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserScore { get; set; }
}
Create an AppSettings helper
public AppSettings
{
private static YourSettingsClass _settings = new YourSettingsClass();
public static string UserFirstName
{
get { return _settings.UserFirstName; }
set { _settings.UserFirstName = value; }
}
public static string UserLastName
{
get { return _settings.UserLastName; }
set { _settings.UserLastName = value; }
}
public static string UserScore
{
get { return _settings.UserScore; }
set { _settings.UserScore = value; }
}
public static void SaveSettings()
{
// Now, use your "settingsfile.xml" (or whatever you're saving as)
// to write your settings to from your _settings static field object.
// I'll let you have a play as to how you want to do this...
}
public static void LoadSettings()
{
YourSettingsClass tempSettingsClass = new YourSettingsClass();
// Now, use your "settingsfile.xml" (or whatever you've saved it as)
// to load in your settings and assign to your tempSettingsClass variable.
// I'll let you have a play as to how you want to do this...
// Assign the settings from your loaded object.
_settings = tempSettingsClass;
}
}
Now, from any other class, you can call AppSettings.LoadSettings(). You could do this on App Startup, or on-demand.
When you've loaded the settings in, just reference AppSettings.UserFirstName or whatever property you want to either get the value or set the value.
When you're ready to, you can then save the settings back to the XML file on disk, through AppSettings.SaveSettings().
I've purposely omitted the code for loading and saving from the storage, and for se/deserializing class objects as I haven't got any UWP components on this PC and I've done this all from memory so I don't want to put anything in to throw you off.
Plus it's a little more learning (even trial/error) for you to do.
Lastly
In the getters for your AppSettings static properties you could also do a null or string.IsNullOrWhiteSpace check for the _settings' property in question, and call the LoadSettings() method if so.
This would save you having to manually call it in-code elsewhere.
Useful links
XmlSerializer and how to use the Serialize method
All about what you can do with the FileIO.WriteTextAsync
Not an article, but a similar question: UWP C# Read & Write XML File
I really hope this helps, somewhat.
Good luck!
I was searching for the Xamarin implementation of How to open settings programmatically
Vito-ziv answered it for objective C - what is the correct way to do this in C# for iOS in Xamarin Studio?
For current devices this is only possible in ios8 (ios9 not available at time of writing) (It used to be possible before ios5 apparently - see this blog post from Adrian Stevens at Xamarin - shout out to him for the inspiration for this answer)
To do it in ios8, I did it like this:
var settingsString = UIKit.UIApplication.OpenSettingsUrlString;
var url = new NSUrl (settingsString);
UIApplication.SharedApplication.OpenUrl (url);
Where the above code was called from a click event via delegate class in a UIAlertView click.
Since I am supporting ios7 too, to handle ios7 devices I did this, where the HandleLocationAuthorisation method is called when deciding whether to present a view controller - the user on ios8 and above can choose to go to the settings directly, whereas the user on ios7 has to go there manually.
This example below is checking for location services, but with trivial changes could easily be changed to check for other types of settings.
public bool HandleLocationAuthorisation ()
{
if (CLLocationManager.Status == CLAuthorizationStatus.AuthorizedAlways) {
return true;
} else {
UIAlertView uiAlert;
//iOS 8 and above can redirect to settings from within the app
if (UIDevice.CurrentDevice.CheckSystemVersion(8,0)) {
uiAlert = new UIAlertView
("Location Services Required",
"",
null,
"Return To App","Open Settings");
uiAlert.Delegate = new OpenSettingsFromUiAlertViewDelegate();
uiAlert.Message = "Authorisation to use your location is required to use this feature of the app.";
//ios7 and below has to go there manually
} else {
uiAlert = new UIAlertView
("Location Services Required",
"Authorisation to use your location is required to use this feature of the app. To use this feature please go to the settings app and enable location services",
null,
"Ok");
}
uiAlert.Show ();
return false;
}
}
For completeness, here is the code for the event delgate referenced above:
public class OpenSettingsFromUiAlertViewDelegate : UIAlertViewDelegate {
public override void Clicked (UIAlertView alertview, nint buttonIndex)
{
if (buttonIndex == 1) {
var settingsString = UIKit.UIApplication.OpenSettingsUrlString;
var url = new NSUrl (settingsString);
UIApplication.SharedApplication.OpenUrl (url);
}
}
}
Hope this will help you. This is working in iPhone not sure about working on iPad.
var url = new NSUrl("prefs:root=Settings");
UIApplication.SharedApplication.OpenUrl(url);
I can't sort this weird issue out and I have tried anything and everything I can think of.
I got 5 pages, everyone of them passing variables with navigation this way:
Pass:
NavigationSerice.Navigate(new Uri("/myPage.xaml?key=" + myVariable, UriKind.Relative));
Retrieve:
If (NavigationContext.QueryString.ContainsKey(myKey))
{
String retrievedVariable = NavigationContext.QueryString["myKey"].toString();
}
I open a list on many pages and one of the pages automatically deletes an item from the list actualProject (actualProject is a variable for a string list). Then, when I go so far back that I reach a specific page - the app throws an exception. Why? I have no idea.
The code that deletes the item:
// Remove the active subject from the availible subjects
unlinkedSubjects.Remove(actualSubject);
unlinkedsubjectsListBox.ItemsSource = null;
unlinkedsubjectsListBox.ItemsSource = unlinkedSubjects;
Then the page that throws the exception's OnNavigatedTo event:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.ContainsKey("key"))
{
actualProject = NavigationContext.QueryString["key"];
try
{
//Read subjectList from IsolatedStorage
subjectList = readSetting(actualProject) != null ? (List<String>)readSetting(actualProject) : new List<String>();
//Put the subjectList into the subjectListBox
subjectListBox.ItemsSource = subjectList;
//Set the subjectsPageTitle to the "actualProject" value, to display the name of the current open project at the top of the screen
subjectsPageTitle.Text = actualProject;
}
catch (Exception)
{
if (language.Equals("en."))
{
// Language is set to english
MessageBox.Show("Couldn't open the project, please try again or please report the error to Accelerated Code - details on the about page");
}
else if (language.Equals("no."))
{
// Language is set to norwegian
MessageBox.Show("Kunne ikke åpne prosjektet, vennligst prøv igjen eller rapporter problemet til Accelerated Code - du finner detaljer på om-siden");
}
}
}
}
Exception:
_exception {System.ArgumentException: Value does not fall within the expected range.} System.Exception {System.ArgumentException}
My theory:
The app kind of loads the currently opened and modified List. Is that possible? No idea.
So there are a number of ways to pass data between pages.
The way you have chosen is the least suggested.
You can use the PhoneApplicationService.Current dictionary but this is messy also if you have a ton of variables, doesn't persist after app shut down and could be simplified.
I wrote a free DLL that kept this exact scenario in mind called EZ_iso.
You can find it here
Basically what you would do to use it is this.
[DataContractAttribute]
public class YourPageVars{
[DataMember]
public Boolean Value1 = false;
[DataMember]
public String Value2 = "And so on";
[DataMember]
public List<String> MultipleValues;
}
Once you have your class setup you can pass it easily between pages
YourPageVars vars = new YourPageVars { /*Set all your values*/ };
//Now we save it
EZ_iso.IsolatedStorageAccess.SaveFile("PageVars",vars);
That's it! Now you can navigate and retrieve the file.
YourPageVars vars = (YourPageVars)EZ_iso.IsolatedStorageAccess.GetFile("PageVars",typeof(YorPageVars));
This is nice because you can use it for more than navigation. You can use it for anything that would require Isolated storage. This data is serialized to the device now so even if the app shuts down it will remain. You can of course always delete the file if you choose as well.
Please make sure to refer to the documentation for any exceptions you have. If you still need help feel free to hit me up on twitter #Anth0nyRussell or amr#AnthonyRussell.info
I'm Using Geckfx22.0 and xulrunner22.0. Since GeckoWebBrowser in .Net shares cookies with all other instances of GeckoWebBrowsers I would like for a GeckoWebBrowser to have it's own cookie container which doesn't share any cookies that was created previously in other GeckoWebBrowsers or other instances.
For example when I create a GeckoWebBrowser it shouldn't have any cookies. And when I run 2 instances of GeckoWebBrowser they have their own cookie container and don't share or conflict cookies with each other.
How is that possible?
I've tried various possible ways by creating different class and initiating geckofx but when running different browser at same time it sharing cookies among other browsers. If i remove cookies from one browser , the same happening for other browsers too. I have initiated the proxy and useragent at different times and its works but cant apply various useragents for multiple browsers at the same time.
public void Initiate()
{
Gecko.Xpcom.Initialize(AppDomain.CurrentDomain.BaseDirectory + "/xulrunner");
if (this.IsProxySet)
{
Gecko.GeckoPreferences.User["network.proxy.http"] = this.Host;
Gecko.GeckoPreferences.User["network.proxy.http_port"] = this.Port;
Gecko.GeckoPreferences.User["network.proxy.type"] = 1;
}
if (IsUseragentSet)
{
Gecko.GeckoPreferences.User["general.useragent.override"] = this.Useragent;
}
}
And to remove cookies i'm using following code :
nsICookieManager CookieMan;
CookieMan = Xpcom.GetService<nsICookieManager>("#mozilla.org/cookiemanager;1");
CookieMan = Xpcom.QueryInterface<nsICookieManager>(CookieMan);
CookieMan.RemoveAll();
Help will be appreciated !!!
You could possibly try implementing your own cookie manager that supports this:
see unittest Register_AfterDefaultFactoryHasBeenUnregistered_NewCookieServiceIsUsedInsteadOfDefaultOne
for an example of how to do this.
This code is currently untested and may contain typeos
This code requires a geckofx version newer than v22.0-0.6
[Guid("c375fa80-150f-11d6-a618-0010a401eb10")]
[ContractID(TestCookieServiceFactory.ContractID)]
public class TestCookieServiceFactory
: GenericOneClassNsFactory<TestCookieServiceFactory, TestCookieService>
{
public const string ContractID = "#mozilla.org/cookieService;1";
}
public class TestCookieService : nsICookieService
{
// Implement nsICookieService...
}
public void Main()
{
Xpcom.Initialize("My Xulrunner/Fireofox location");
var existingFactoryDetails = TestCookieServiceFactory.Unregister();
TestCookieServiceFactory.Register();
var browser = new GeckofxWebBrowser();
// Add browser to form etc...
browser.Navigate("http://SomeWebPageThatUsesCookies")
// Cookie requests should now be sent to TestCookieService, process them as your want.
}