I am starting in WP development but I already got the following situation.
I have my app and I need other apps to call it passing data/parameters that I will specify.
For example:
App X will call and open my app, passing parameters like id, name and age.
Is there any way to do it?
Define a URI association for your app.
To do so add content like this to the app's manifest file:
<Extensions>
<Protocol Name="myapp" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
</Extensions>
Define a class that derives from UriMapperBase to handle incoming URIs and register right after the call to RootFrame.Navigated + = /*...*/ by doing this:
RootFrame.UriMapper = new MyPersonalUriMapper();
Other apps can open your app by doing something like this:
Windows.System.Launcher.LaunchUriAsync(new System.Uri("myapp:4711/Luciano/24"));
You can parse this URI in your UriMapper to open the page you wanted with ID=4711, Name=Luciano and Age=24.
Related
Today I've got a question about saving a .csv or .txt file within a Xamarin app on UWP platform. I am trying to save a file I create in my code call tags.csv. My goal is to have no .csv's saved initially, I create an instance in my code, then save it and create a new .csv file when my code executes. The creation and filling of the .csv occurs in one function which triggers based on a Button instance in my app. Also, ideally I could make it save in a location determined by a file explorer popup.
I have tried two routes so far to make and save a .csv file, the CSVExport package and CSVhelper package. Both I have been able to download and add to my project from NuGet successfully.
I have tried separately a simple implementation of each, basically just taking their Example code to see if it would work in my UWP app. Here is the respective code
// CSVExport methods, two ways to save
var myExport = new CsvExport();
...
File(myExport.ExportToBytes(), "text/csv", "results.csv"); // method 1
myExport.ExportToFile("./results.csv"); // method 2
// CSVhelper method
var records = new List<Foo>
{
new Foo { Id = 1, Name = "one" },
};
using (var writer = new StreamWriter("./tags.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(records);
}
Here is the error I am receiving: System.UnauthorizedAccessException: 'Access to the path C:...(filepath)...BLE.Client.UWP\bin\x86\Debug\AppX\tags.csv' is denied.'
Whenever the code reaches my saving of the .csv file, it crashes the app and Visual Studio 2022 gives me this error message. The same exact error occurs whether I am using CSVExport or CSVhelper.
Attempted Solutions:
My attempted solutions are mainly in regards to giving the app the permissions it needs to save. If an alternative like getting a different CSV package is better, I would take that advice too.
One solution I saw on StackOverflow linked to this page. The issue is I cannot load StorageFolder or Windows.Storage in my Xamarin app, it just won't recognize it and won't compile cause it's a missing load action.
Another solution I saw was changing your Capabilities in the Package.appxmanifest file and changing your Package header. I have done so, so mine looks like the following code sample. I need the internetClient and bluetooth and location for the app itself, so I added broadFilesystemaccess and even documents and pictures just to see if that would work too.
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
...
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="broadFileSystemAccess" />
<uap:Capability Name="documentsLibrary"/>
<uap:Capability Name="picturesLibrary" />
<DeviceCapability Name="bluetooth" />
<DeviceCapability Name="location"/>
</Capabilities>
Another solution was making sure the UWP app had permissions, which I went into system settings and allowed, so it should have full access now.
I am not sure where to go from here, so any advice about UWP or saving files within Xamarin UWP apps would be appreciated.
Based on your requirement, you could try to use the DependencyService feature of Xamarin.Forms.
DependencyService enables you to invoke native platform functionality from shared code. For your scenario, you could pass the stream of the file to the DependencyService first. then you could call the UWP FileSavePicker using DependencyService in your Forms app and save the stream as a file.
Here are some code snippets about how to implement the interface.
SaveFileAsync
public async Task SaveFileAsync(Stream data)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Text", new List<string>() { ".txt" });
savePicker.FileTypeChoices.Add("CSV", new List<string>() { ".csv" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "New Document";
StorageFile file = await savePicker.PickSaveFileAsync();
if (file!= null)
{
using (IRandomAccessStream dataStream= data.AsRandomAccessStream())
{
using (var reader = new DataReader(dataStream.GetInputStreamAt(0)))
{
await reader.LoadAsync((uint)dataStream.Size);
var buffer = new byte[(int)dataStream.Size];
reader.ReadBytes(buffer);
await Windows.Storage.FileIO.WriteBytesAsync(file, buffer);
}
}
}
}
ISaveFileServicecs interface
public interface ISaveFileServicecs
{
Task<Stream> SaveFileAsync(Stream stream);
}
Usage
await DependencyService.Get<ISaveFileServicecs>().SaveFileAsync(stream);
I share translations for my iOS and Android apps through a common PCL that has AppResources.resx files. I access my resource strings in C# code like AppResources.MyString.
This has worked well up until now, when I have introduced an accessibility service into my android app.
With the accessibility service, I need to provide a description. The only documentation I can find always shows this description being set in the accessibilityservice.xml file such as:
<?xml version="1.0" encoding="utf-8" ?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeWindowStateChanged|typeWindowContentChanged"
android:accessibilityFeedbackType="feedbackSpoken"
android:accessibilityFlags="flagDefault"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:description="#string/accessibility_service_description" />
Which is bound to the service like:
[Service(Permission = "android.permission.BIND_ACCESSIBILITY_SERVICE", Label = "MyApp")]
[IntentFilter(new string[] { "android.accessibilityservice.AccessibilityService" })]
[MetaData("android.accessibilityservice", Resource = "#xml/accessibilityservice")]
public class AutofillService : AccessibilityService
{
...
}
Obviously I cannot access AppResources.MyAccessibilityDescString from the accessibilityservice.xml file, so how do I provide my translatable AppResources string to the accessibility service description?
I would hate to have to manage other android specific strings in the android project only instead of having a common place my translators can set all strings for the app (AppResources.resx).
You can not get the AppResources.resx content in xml file. And unfortunately the Description property of AccessibilityServiceInfo is read only. So you can not set the value by SetServiceInfo method.
The only option I can think of is to add the description string in the String.xmlfile.
I have to open an app from another app in Xamarin forms. I found the Rivets component which says it can do what i want but I'm not sure what url it refers to.
I looked at http://applinks.org/documentation/ and it says the url I want opened per mobile platform.
My question is, for iOS, what url is it looking for? I thought it was the link to the app in itunes. All that link does is open the app store at the app but doesn't launch the app if it's already installed.
Unless you have control over the Blue Diamond Party app's code or you know that the Blue Diamond Party app has registered one or more custom app links (which are the custom URLs you have read about), you will not be able to open it from your app on iOS. This is the only way to make that work with Rivets. The Blue Diamond Party app would need to register a custom URI of some kind that it would respond to. Then, using Rivets, your app would call that custom URI.
*Edit: To get your app to handle app links from other apps, you can head to the Xamarin's Getting Started Guide (look under the Handling Incoming App Link Navigation heading):
Android does things different than iOS, but for iOS:
Register the custom URI in the Info.plist (not sure if that is the correct XML but it is something like that, check out the link for an image)(the string under CFBundleURLName is just a custom name for your scheme and then you can list multiple schemes in the array under the name):
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.bluediamondparty.schemes</string>
<key>CFBundleURLSchemes</key>
<array>
<string>bluediamondparty</string>
</array>
</dict>
</array>
The above XML would register the bluediamondparty as your URI scheme for your app to respond to.
Now, in order for your app to do something when a URI with your custom scheme is run, you override OpenUrl in your AppDelegate (this allows you to put things after your custom URI scheme to, for example, open a specific page or item in your app):
public override bool OpenUrl (UIApplication app, NSUrl url, string srcApp, NSObject annotation) {
var rurl = new Rivets.AppLinkUrl (url.ToString ());
if (rurl.InputUrl.Host.Equals ("play")) {
var gameModeType = rurl.InputQueryParameters ["game_mode_type"];
var c = new ProductViewController (gameModeType, rurl.Referrer);
navController.PushViewController (c, true);
return true;
}
return false;
}
To open the URI from your second app, you might run something like this:
UIApplication.SharedApplication.OpenUrl(new NSUrl("bluediamondparty://play?game_mode_type=survival"));
Finally, you might notice that none of that actually required using the Rivets component... so what does that component actually do for us?! What Rivets actually does is to allow your app to pass in a real URL (such as http://rdio.com/song/12345) and Rivets will go to that page and look for special meta tags that describes how the content on the page is linked to a mobile app that the user might have installed on their device. So on that rdio web page they might have the following meta tag:
<meta property="al:ios:url" content="rdio://song/12345" />
In that meta tag it will list the correct custom URI scheme to attempt to open the Rdio app if the user has it installed and it has registered itself with the custom rdio URI scheme.
The other thing to know is that your app does not have exclusive rights to a URI scheme. So if another app chooses the same URI scheme as you and the user installs both of them, then the last app to be installed will respond to the URI scheme.
I have an app that needs to include a links to a second app in the same phone.
If the app is not installed the link should point to the windows store to install it (that part is working fine).
But if the app is already installed the link should go straight to the app and open it. How can I do that?
The app has two versions one form WP7 and other from WP8. if the solution is different for them please point the difference.
Thanks for the help...
I believe a URI Association is what you want. You should be able to create a different association in your WP7 app and in your WP8 app, and handle them accordingly.
A URI association allows your app to automatically launch when another app launches a special URI.
Also note:
If you are interested only in launching your own apps, consider using
APIs from the Windows.Phone.Management.Deployment namespace. You can
use this API to check for other apps that you’ve published, and then
launch them if they’re installed.
You basically just need to update the WMAppManifest.xml file to include the URI Association and then listen for that URI. Example:
<Extensions>
<Protocol Name="contoso" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
</Extensions>
Then you can use a custom URI Mapper to handle your association (full example in top link above):
public override Uri MapUri(Uri uri)
{
tempUri = System.Net.HttpUtility.UrlDecode(uri.ToString());
// URI association launch for contoso.
if (tempUri.Contains("contoso:ShowProducts?CategoryID="))
{
// Get the category ID (after "CategoryID=").
int categoryIdIndex = tempUri.IndexOf("CategoryID=") + 11;
string categoryId = tempUri.Substring(categoryIdIndex);
// Map the show products request to ShowProducts.xaml
return new Uri("/ShowProducts.xaml?CategoryID=" + categoryId, UriKind.Relative);
}
// Otherwise perform normal launch.
return uri;
}
Hope this helps!
Is the secondary app one that you have created? If so, do something like this:
IEnumerable<Package> packages = InstallationManager.FindPackagesForCurrentPublisher();
foreach (Package package in packages)
{
if (package.Id.ProductId.ToString().ToLower() == "product id of secondary app")
{
//Launch the app
package.Launch();
}
}
Make sure that your publisher ids match in the WMAppManifest for both apps.
If this secondary app was published by someone else, you'll need to use a custom Uri schema. The app needs to have this feature added by the developer, you can't just launch any app.
I successfully deploy my custom Action to the list of Actions available for use in my SharePoint Designer, but when opening an existing workflow, or creating a new one in the Designer, I get the message (and of course my custom action is not on the list of actions)
The list of workflow actions on the server references an assembly that
does not exist. Some actions will not be available. The assembly
strong name is {Actual Assembly strong name}. Contact your server
administrator for more information.
I checked the Strong Assembly name, Global Assembly Cache, package options, .ACTIONS file, web.config... Everything seems ok. Any new Ideas?
I am assuming the custom action is a farm deployed activity, which inherits from System.Workflow.ComponentModel.Activity (perhaps using subclass SequenceActivity, but really that doesn't matter)
I'm guessing that you haven't created the required ACTIONS file, which gets deployed to TEMPLATE\1033\Workflow
<?xml version="1.0" encoding="utf-8" ?>
<WorkflowInfo>
<Actions Sequential="then" Parallel="and">
<Action Name="Description for SP Designer"
Assembly="$SharePoint.Project.AssemblyFullName$"
ClassName="AssemblyName.ClassName"
AppliesTo="all"
Category="SPD category"
UsesCurrentItem="true"
>
<RuleDesigner Sentence="Line as it appears in SPD workflow" />
<Parameters>
<Parameter Name="__ActivationProperties" Type="Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties, Microsoft.SharePoint" Direction="In" />
</Parameters>
</Action>
</Actions>
</WorkflowInfo>
SPD reads the list of activities from the ACTIONS files. Adding the file will get it into the menu. To actually add it to the workflow, you also need to authorize the custom workflow activity by class name.
To add the authorized type, I use a feature receiver with the following spwebmodification:
private SPWebConfigModification CreateWebConfigModification(string assembly, string assemblyNamespace)
{
return new SPWebConfigModification()
{
Type = (SPWebConfigModification.SPWebConfigModificationType)0,
Name = String.Format("authorizedType[#Assembly='{0}'][#Namespace='{1}'][#TypeName='*'][#Authorized='True']", (object)assembly, (object)assemblyNamespace),
Path = "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes",
Owner = assemblyNamespace,
Sequence = 0U,
Value = String.Format("<authorizedType Assembly='{0}' Namespace='{1}' TypeName='*' Authorized='True' />", (object)assembly, (object)assemblyNamespace)
};
}
this will generate an SPWebConfigModification which can be used during install/uninstall.
Check you local admin privleges. This error comes up if you don't have local priveleges
create a new web and site collection and create a new a new workflow for the new site. you'll get the error message. don't save the work flow. and close the SPD.
reopen the designer and create a new work flow it'll solve the problem.