Here i am trying to access the appsettings.json from the Asssembly but it only shows the default loaded files as if the appssetings.json didn't exist in the same path
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
builder.Services.AddSingleton<SignalrMessenger>();
Assembly a =Assembly.GetExecutingAssembly();
using Stream str = a.GetManifestResourceStream("MauiAppClient.appsettings.json");//null
var x =Assembly.GetExecutingAssembly().GetManifestResourceNames();//list everything but the appsettings.json {App.xaml, MainPage.Xaml,AppShell.xaml}
return builder.Build();
}```
so how do you register the appssetings file?
Related
The goal is to have a Maui class library that will have a function that return a full path to read a text file in my Maui application.
Can you help me to fix the following code?
The error is when I try to return the FullPath as string
var FileFullPath = await result.FullPath.ToString();
Here the error description
Severity Code Description Project File Line Suppression State Error CS1061
'string' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) MPCFilePickerMauiLibrary (net7.0), MPCFilePickerMauiLibrary (net7.0-android), MPCFilePickerMauiLibrary (net7.0-ios), MPCFilePickerMauiLibrary (net7.0-maccatalyst) D:\repos\MPC-MassPropertiesCalculator\MPCFilePickerMauiLibrary\PickTxtFile.cs 35 Active
Here is the Maui class library that have created.
using Microsoft.Maui.Storage;
namespace MPCFilePickerMauiLibrary;
//Ref https://youtu.be/C6LV_xMGdKc - Intro To Class Libraries in C#
public class PickTxtFile
{
public static async Task<string> GetFilePathAsync()
{
//For custom file types
var customFileType = new FilePickerFileType(
new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { "public.text" } }, // UTType values
{ DevicePlatform.Android, new[] { "text/plain" } }, // MIME type
{ DevicePlatform.WinUI, new[] { ".Txt" } }, // file extension
{ DevicePlatform.Tizen, new[] { "*/*" } },
{ DevicePlatform.macOS, new[] { "Txt" } }, // UTType values
});
var result = await FilePicker.PickAsync(new PickOptions
{
PickerTitle = "Pick MPC Demo file Please",
FileTypes = customFileType
});
if (result == null)
return "";
var FileFullPath = await result.FullPath.ToString();
return FileFullPath;
}
Here is the code where I'm using the function
using MPCFilePickerMauiLibrary;
using Microsoft.UI.Xaml.Controls;
namespace MPC_MassPropertiesCalculator_MAUIapp.Views;
public partial class MPCFileDemo : ContentPage
{
public MPCFileDemo()
{
InitializeComponent();
}
private void MenuFlyoutItem_Clicked(object sender, EventArgs e)
{
String filePath = PickTxtFile.GetFilePathAsync();
if (File.Exists(filePath))
{
//TODO Read file
}
}
}
With Jason's comments, here is the solution.
Maui class library
using Microsoft.Maui.Storage;
namespace MPCFilePickerMauiLibrary;
//Ref https://youtu.be/C6LV_xMGdKc - Intro To Class Libraries in C#
public class PickTxtFile
{
public static async Task<string> GetFilePathAsync()
{
//For custom file types
var customFileType = new FilePickerFileType(
new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { "public.text" } }, // UTType values
{ DevicePlatform.Android, new[] { "text/plain" } }, // MIME type
{ DevicePlatform.WinUI, new[] { ".Txt" } }, // file extension
{ DevicePlatform.Tizen, new[] { "*/*" } },
{ DevicePlatform.macOS, new[] { "Txt" } }, // UTType values
});
var result = await FilePicker.PickAsync(new PickOptions
{
PickerTitle = "Pick MPC Demo file Please",
FileTypes = customFileType
});
if (result == null)
return "";
var FileFullPath = result.FullPath.ToString();
return FileFullPath;
}
}
Code in the application
using MPCFilePickerMauiLibrary;
namespace MPC_MassPropertiesCalculator_MAUIapp.Views;
public partial class MPCFileDemo : ContentPage
{
public MPCFileDemo()
{
InitializeComponent();
}
private async void MenuFlyoutItem_Clicked(object sender, EventArgs e)
{
string? filePath = await PickTxtFile.GetFilePathAsync();
if (File.Exists(filePath))
{
await DisplayAlert("File Path", $"File Path is: {filePath}", "OK");
}
else
{
await DisplayAlert("File Path", "Usre did not select a file", "OK");
}
}
}
Output result image.
I am a newbie with Xamarin Forms and Firebase.
I am writing a command to get data on Firebase Realtime Database.
Here is my "Main" code:
authProvider = new FirebaseAuthProvider(new FirebaseConfig(ApiKey));
auth = authProvider.SignInWithEmailAndPasswordAsync("email", "pass").Result;
auth = Task.Run(() => auth.GetFreshAuthAsync()).Result;
firebase = new FirebaseClient(
"https://xxxxxxxxxxx.firebaseio.com/",
new FirebaseOptions
{
AuthTokenAsyncFactory = async () => (await auth.GetFreshAuthAsync()).FirebaseToken
});
var items = GetAllItem(firebase);
Here is my "GetAllItem()" code:
public static async Task<List<Person>> GetAllItem(FirebaseClient firebaseClient)
{
return (await firebaseClient
.Child("Persons")
.OnceAsync<Person>()).Select(item => new Person
{
Name = item.Object.Name,
}).ToList();
}
When I run my code, the result that I get like this image with "Status" = "WatingForActivation":
https://i.stack.imgur.com/YfEe0.png
My Firebase DB' rule like this:
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules.
*/
"rules": {
".read": "auth==null",
".write":"auth==null"
}
}
So, any solution to fix this problem?
Change:
var items = GetAllItem(GetAllItem);
To:
var items = await GetAllItem(GetAllItem);
If you need more code, you could download my project from FirebaseDatabase/XamarinFirebase folder on GitHub.
https://github.com/WendyZang/Test.git
I have an Office Addin and am trying to update the title of the document on desktop. i have tried 2 diffrent ways and none of them works on hte desktop. It works fine on word online but not on the desktop.
Word.run(async (context) => {
var newTitle = document.getElementById("inputTitle") as HTMLInputElement;
console.log(newTitle.value);
context.document.properties.title = newTitle.value;
});
This code works online but not on the desktop. I have also tried doing doing it in this way.
Office.context.document.customXmlParts.getByNamespaceAsync("http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
function (resultCore) {
var xmlPart = resultCore.value[0];
xmlPart.getNodesAsync('*/*', function (nodeResult) {
for (var i = 0; i < nodeResult.value.length; i++) {
var node = nodeResult.value[i];
console.log("BaseName: ")
console.log(node.baseName);
if (node.baseName === "title") {
var newTitle = document.getElementById("inputTitle") as HTMLInputElement;
console.log("title that you entered: " + newTitle.value);
console.log(node);
node.setNodeValueAsync(newTitle.value, { asyncContext: "StateNormal" }, function (data) { });
}
}
});
});
Does anyone know why it doesn't work or have some other solution to my problem?
The following code works for me, including on desktop. Note that you have to await the Word.run. Also, you have to load the title and then sync to make sure you have changed the title on the actual document and not merely in the proxy object in your task pane code.
await Word.run(async (context) => {
var newTitle = document.getElementById("inputTitle") as HTMLInputElement;
console.log(newTitle.value);
context.document.properties.title = newTitle.value;
const myProperties = context.document.properties.load("title");
await context.sync();
console.log(myProperties.title);
});
I am developing web api(s) using Asp.net core 2.0. Now the thing I need is to use different connection strings which will be specified in the headers of the user.
I have previously seen this (not directly about this question):
app.Use(async (context, next) =>
{
if (string.IsNullOrWhiteSpace(context.Request.Headers["Authorization"]))
{
if (context.Request.QueryString.HasValue)
{
var token = context.Request.QueryString.Value
.Split('&')
.SingleOrDefault(x => x.Contains("authorization"))?.Split('=')[1];
if (!string.IsNullOrWhiteSpace(token))
{
context.Request.Headers.Add("Authorization", new[] { $"Bearer {token}" });
}
}
}
await next.Invoke();
});
But how should I change the connection string in a way like this?
Am I going in the right direction?
Update
I am looking for a middle ware like this:
app.Use(async (context, next) =>
{
if (!string.IsNullOrWhiteSpace(context.Request.Headers["TestMode"]) && context.Request.Headers["TestMode"] == true)
{
// TODO Change the connection string here
}
await next.Invoke();
});
But don't know what to put in the TODO.
I been working with fileTransfer from Ionic native, it working fine on Android, but on iOS not not do anything at all
Here is my code:
getFile(fileName,url){
let fileTransfer: FileTransferObject = this.transfer.create();
fileTransfer.download(url, this.file.dataDirectory + fileName).then((entry) => {
console.log('entry:',entry)
}, (error) => { console.log(error); });
console.log('file transfer',fileTransfer.onProgress)
var a = fileTransfer.onProgress((progressEvent: ProgressEvent) => {
this.ngzone.run(() => {
console.log(progressEvent.loaded)
});
});
}
it depends on how to derive this.file.dataDirectory.
In my case it always works like this ..
declare var cordova:any; //on top of file
if (this.platform.is('android')) {
this.fsurl = cordova.file.dataDirectory;
}else{
this.fsurl = cordova.file.documentsDirectory;
}