Android/Xamarin/C#: Resource Id not constant anymore - c#

I used to work on an Android/Xamarin project for Android 4.4, API Level 19 that contains an activity with the following code:
private void RadioButtonClick(object sender, EventArgs e)
{
RadioButton rb = (RadioButton)sender;
switch (rb.Id)
{
case Resource.Id.radioButton1:
...
The last time I looked at the project a couple of month ago, this used to work just fine. Now I want to continue working on the project for Android 8.0, API Level 26, but when I opened it in Visual Studio 2017 I got the compiler error (field)int ResourceId.RadioButton1 A constant value is expected.
Now this is not a problem in itself, as I can always use if (rb.Id == ...) instead of switch/case, but I would like to know why all of a sudden Resource.Id.radioButton1 is not recognized as a constant anymore, even though it is declared as such in Resource.Designer.cs:
public const int radioButton1 = 2131165244;
Has anyone else encountered something similar?
EDIT: Even stranger is that the project builds without complaint, but deployment is not possible due to the said error.
EDIT: It seems that the build process generates another Resource.Designer.cs file which is put into a folder obj\Debug\designtime. Once the file exists, the activities source code refers for the Id declarations to this new file and not to the original Resource.Designer.cs in the Resources folder.
However, all Ids in the new file are static and not const (which of course is the reason for the compiler error that prompted this question in the first place) and moreover they are all set to zero.
Are there any Android/Xamarin experts out there who can explain what is going on here? Might that even be a bug in Visual Studio 2017/Xamarin?
EDIT: Here is a link shedding some light on this topic: https://forums.xamarin.com/discussion/19369/compiling-resource-ids-static-vs-const
What I don't get is this: Why did the above code work at all considering that this problem is known since 2014?

Related

iOS iCloud failing to write with, Couldn't get container configuration from the server error

I have one app that I wrote already reading from and writing to the iCloud. I am essentially using the same code in my new app to do the same thing, but for some reason it will not work, giving me the following error, "Couldn't get container configuration from the server". Let me clarify, with this new app it does puts an entry in iCloud under manage storage, but instead of being under the name of my app, it is under adhoc.
Here is the line in my info.Plist,
Here is the line from my Entitlements,plist
lastly, here is my identifier defined on the apple developer site,
I have verified and reverified that everything is pointing to the correct thing so I am baffled. Any help would be much appreciated.
EDIT I guess what it is doing is writing file to my phone, but when it goes to save data to it, it fails with this message, Here is my call to save the data.
CKRecordID recordID = new CKRecordID(strDate);
await Task.Delay(200);
// Save it to iCloud
await pvc.SaveToiCloud(newRecord);
Here is my code to save the record,
public async Task SaveToiCloud(CKRecord newRecord)
{
ThisApp.PrivateDatabase.SaveRecord(newRecord, (record, err) =>
{
Edit:
I was thinking that possibly the number of nodes I had was too many, so I took out the "dist" one you see below, but that did not help. I thought maybe that was why I was seeing module name of adhoc under icloud on my phone, but I guess I was wrong.
Old:
New:
Edit
I have been doing more digging and found that this line of code is actually the one throwing the error.
File.WriteAllText(Path.Combine(filePath.Path, name + date), "Test");
The name and date contain correct values and the path looks fine to me... I guess... Don't know actually how it should look. Here is how the file path is getting set right above this call,
NSUrl filePath = NSFileManager.DefaultManager.GetUrlForUbiquityContainer(null).Append("Documents", true);
If anyone could offer any advice, I would be most appreciative.
So, I finally figured out the issue and it works like a champ now. The issue was the case in my bundle id in my info.plist. In CloudKit the DB name was all lower case, but my bundle Id in my info.plist, had AdHoc as my last node instead of adhoc as was in the CloudKit. You might say, what does the bundle Id have to do with the iCloud name, and actually I am not really sure, but I noticed that it was taking the case of that last node from my Bundle Id, not the case specified in the iCloud definition as I have shown above. Hope this helps someone who is struggling with a similar issue Have a great day!

Visual Studio 2019 IntelliSense changed (how to get prior behavior?)

Since I switched to VS 2019 (from 2017) I am pretty sure I got a degraded IntelliSense experience. I looked for settings under Tools -> Options... but did not find anything helpful.
The matter is this: suppose I have a variable kvp that has a property Key, I could type "key.", scroll to the "Key" property (if this is not already selected) and then type ";" to complete the statement.
But this does not work anymore. Instead it now ignores what I selected and I get
key.;
The only way to get the selected property is to explicitly hit enter. Same when I type the first character(s) to select the desired option: when pressing ; it just leaves what I already typed and adds the ; immediately behind it, ignoring what I selected in the popup menu.
What happened and how do I get the proper behavior back?
I am using the Preview version but I already had a couple of updates and it does not improve so I guess it is by design or default behavior now.
Here's some of my code for Perry. It is just an example though, the problem (or what I regard to be a problem) occurs with any object variable.
private static void AddBlockNodes(TreeNode node, IDictionary<string, Block> blocks)
{
foreach (KeyValuePair<string, Block> kvp in blocks)
{
string name = kvp.Key;
Block block = kvp.Value;
TreeNode childNode = new TreeNode(name);
childNode.Tag = block;
node.Nodes.Add(childNode);
AddBlockNodes(childNode, block.Subblocks);
}
}
it just leaves what I already typed and adds the ; immediately behind
it, ignoring what I selected in the popup menu.What happened and how
do I get the proper behavior back?
It is quite an abnormal behavior and l have installed Visual Studio 16.6.0 Preview 2.1 and test your code in my side and it works well.
Type variable kvp. and then select property Key and it types as expected. I did not face missing property Key during the process.
You can try these steps to troubleshoot your issue:
Suggestion
1) reset all settings by Tools-->Import and Export Settings-->Reset all settings
2) close VS Instance, enter the project path and delete .vs hidden folder which stores some Intellisense settings, bin, obj folder and then restart your project again. I wonder if you migrate an old project into VS2019 preview version, I think you should complete this step.
3) disable any third party extensions if you have under Extensions-->Manage Extensions in case they cause this behavior.
4) delete all component caches under C:\Users\xxx\AppData\Local\Microsoft\VisualStudio\16.0_xxx(16.0 is VS2019)\ComponentModelCache
5) try to create a new project in VS2019 Preview version and test whether this issue persists in the new platform and if it works, I think it is an issue of your project itself. Or you can try to migrate your project into the new project.
Hope it could help you.

I cannot get Xamarin to play small media files

Using an android scanner device, running KitKat.
Using Xamarin in Visual Studio Enterprise 2017, 15.9.9.
I need to generate a "Success" or "Error" sound, based on the content of the scanned barcode.
I have two files: "Success.mp3" and "Error.wav", neither of which will play.
Since this should be a very simple process, I am trying to use Android's MediaPlayer class, rather than add some NuGet package.
I am using Dependency Injection to properly run the Android code, since Xamarin does not have any sort or media API.
I instantiate Android's MediaPlayer as variable "player", and it does successfully instantiate, but as soon as I attempt to do anything with it, it throws a Null Exception error and the value of "player" displays as Null.
I have been experimenting with different ways to do this and have copies of the sound files stored both in the Assets folder, and the Resources/Raw folder (see below).
Here is my method:
public void PlaySound(string soundType) {
var filename =
global::Android.App.Application.Context.Assets.OpenFd(soundType);
if (player == null) {
MediaPlayer player = new MediaPlayer();
}
//This is where the error happens
player.SetDataSource(filename);
player.Prepare();
player.Start();
}
I have also tried the last three lines as the following, with the same result:
player.Prepared += (s, e) => {
player.Start();
};
player.SetDataSource(filename.FileDescriptor, filename.StartOffset,
filename.Length);
player.Prepare();
I have also attempted to utilize what so many people demonstrate as the way to do this, but it does not work for me. This is where the file must be stored in Resources/Raw:
player = MediaPlayer.Create(global::Android.App.Application.Context,
Resource.Raw.someFileName);
Whatever value that you use for "someFileName", all Visual Studio gives you is "'Resource.Raw' does not contain a definition for 'someFileName'".
Resource.designer.CS does contain entries for both files:
public const int Error = 2131230720;
public const int Success = 2131230721;
Expected results: sound, or some meaningful error message that puts me on the right path.
I am still relatively new to Xamarin and am probably missing something that would be obvious to veteran eyes. I have tried so many other things, most of which are not mentioned here, grasping for some straw. This should be simple, but is proving otherwise. Thank you for any help that you can provide.

WPF Live-Regions just for Narrator?

I have a textblock (ContentTextBlock) with AutomationProperties.LiveSettings="Assertive". I'm just testing and checking how useful this feature is. And... am disappointed so far.
private void Button_Click(object sender, RoutedEventArgs e)
{
ContentTextBlock.Text += " test";
var peer = UIElementAutomationPeer.FromElement(ContentTextBlock);
if(peer == null)
{
peer = UIElementAutomationPeer.CreatePeerForElement(ContentTextBlock);
peer.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged);
}
peer.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged);
}
When using Narrator, this works as advertised. Whenever clicking the button, Narrator announces the TextBlock text ("test" "test test" "test test test")... But when I use NVDA or JAWS, nothing happens, although the screenreader versions are relatively up-to-date. Did they really not add any support for live-regions or am I just missing an important point?
Whilst I didn't manage to get live regions to work, I found another workaround:
Tolk by Davy Kager
Tolk is a library which can, among oterrs
Detect which supported screen reader, if any, is running
Pass strings to the screen reader's speech engine and braille.
Also has support for SAPI
To include Tolk in your C# project, download it from the link above, then include tolk.cs (from src/dotnet) in your project, and place tolk.dll (it's found in bin) in the folder with your executable (or somewhere in the PATH variable). Make sure that the dll version matches your CPU target (x86/x64). Do the same for the dlls in the lib directory. Then you can use it according to the code found in the examples folder.
PS. Tolk works on Win 7 as well, so that's a bonus. The live-regions of WPF were only supported from Win 8 on.

Errors saving app settings in C#

I know this has been answered elsewhere, but I can't get the basics to run...
Regardless of the application purpose (which is obvious)
private void btnSave_Click(object sender, EventArgs e)
{
// these are *internal* properties of the NET appilcation
FileSystemWatcher.Properties.Settings.Default.MailServer = Convert.ToString(txtMailServer);
FileSystemWatcher.Properties.Settings.Default.ServerPort = Convert.ToInt16(txtServerPort.Text);
FileSystemWatcher.Properties.Settings.Default.Subject = Convert.ToString(txtSubject);
FileSystemWatcher.Properties.Settings.Default.MessageFrom = Convert.ToString(txtMessageFrom);
FileSystemWatcher.Properties.Settings.Default.MessageTo = Convert.ToString(txtMessageTo);
FileSystemWatcher.Properties.Settings.Default.Save();
}
First issue is that I get a bunch of these exceptions when I hit this code...
Exception thrown: 'System.Security.SecurityException' in mscorlib.dll
No other runtime errors I can see...
The app.config appears to be structured correctly - using Project-Add New Item.. etc and inspecting the app.config contents.
But the SAVE function listed above - doesn't save any changes I may make.
thanks
I discovered my folly... BEFORE I even knew there was a FileSystemWatcher class - I had already named my project ... you guessed it.. FileSystemWatcher !
So the namespace assumed I was talking about the class - not the app framework !
No compile errors of course!
THANKS to all the helpers!
Forgive me - I'm learning!

Categories

Resources