在C#/ XAML的Windows 8应用程序中,我有时想从非异步方法调用一个等待的方法.
实际上替换它是正确的:
public async Task<string> MyCallingMethod()
{
string result = await myMethodAsync();
return result;
}
Run Code Online (Sandbox Code Playgroud)
这样 :
public string MyCallingMethod()
{
Task.Run(async () => {
string result = await myMethodAsync();
return result;
});
}
Run Code Online (Sandbox Code Playgroud)
对我来说的好处是我可以在没有等待的情况下使用MyCallingMethod,但这是正确的吗?如果我想为MyCallingMethod传递ref参数,这可能是一个优点,因为在异步方法中不可能有ref参数.
对于Windows 8应用程序,在C#/ Xaml中我尝试注册后台任务.这很难说,但我想我的后台任务已被注册,但当我在调试位置工具栏上点击后台任务的名称时,我的应用程序停止工作,没有任何消息.
我查看了事件查看器上的日志,我得到:"带有入口点MyApp.Pages.SampleBackgroundTask和名称示例每小时后台任务的后台任务无法激活,错误代码为0x80010008."
这是我的代码:
private async void Button_Click_BackgroundTask(object sender, RoutedEventArgs e)
{
string entryPoint = "MyApp.Pages.SampleBackgroundTask";
string taskName = "Example hourly background task";
TimeTrigger hourlyTrigger = new TimeTrigger(60, false);
BackgroundTaskRegistration myBackGroundTaskRegistration = RegisterBackgroundTask(entryPoint, taskName, hourlyTrigger, null);
if(myBackGroundTaskRegistration != null)
AttachProgressAndCompletedHandlers(myBackGroundTaskRegistration);
}
public static BackgroundTaskRegistration RegisterBackgroundTask(String taskEntryPoint,
String name,
IBackgroundTrigger trigger,
IBackgroundCondition condition)
{
var taskRegistered = false;
var iter = Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks;
foreach (var item in iter)
{
var task = item.Value;
if (task.Name == name)
{
taskRegistered = true; …Run Code Online (Sandbox Code Playgroud) 我创建了一个带有项模板和标题模板的Grouped GridView.它运作良好但我希望我的列表的第一篇文章有一个不同的模板(更大).就像法国应用程序"LeMonde"一样.我不知道如何定义模板来实现这一目标.
这是我目前的xaml代码
<Page.Resources>
<CollectionViewSource x:Name="cvs1" IsSourceGrouped="True" />
</Page.Resources>
<Grid Background="White">
<GridView x:Name="PicturesGridView" SelectionMode="None"
ItemsSource="{Binding Source={StaticResource cvs1}}"IsItemClickEnabled="True" ItemClick="ItemView_ItemClick">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="RectanglesStackPanel" Margin="8" Orientation="Vertical" Width="242">
<Image Source="{Binding imageUrl}" Height="180" Width="225" Stretch="UniformToFill" />
<Border Background="Gray" Opacity="1" Width="225" Height="115">
<TextBlock Text="{Binding title}"
Foreground="White" TextWrapping="Wrap" Width="215" FontSize="18" />
</Border>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Button Click="Button_Click_1" Content="{Binding Key}" Foreground="Black" Background="White" FontSize="30" Margin="0,0,0,-10" ></Button>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" />
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle> …Run Code Online (Sandbox Code Playgroud) 对于C#/ XAML中的Windows 8应用程序,我需要访问特定的ressource文件.在WP7中,我使用了resx文件,现在看来我们需要使用resw文件.它不是语言资源文件.我的文件名为ConfigResources.resw,它只包含一个键:"ConfigFile"和一个值:一个字符串.
如何从我的代码中访问它?我试了一下没有运气:
var storedConfigFile = Application.Current.Resources["ConfigResources"];
Run Code Online (Sandbox Code Playgroud)
那么如何从我的代码中编辑内部键的值?
谢谢
对于C/XAML中的Windows 8应用程序,我必须将一些数据存储到LocalState文件夹中.我认为使用async/await运算符时遇到了一些麻烦.
当我在应用程序运行时保存数据时,一切正常,但是当我尝试在OnSuspending方法中保存数据时,似乎我的应用程序不会等待我的保存方法完成以暂停应用程序.
更奇怪的是,当我正在调试并逐步完成所有操作时,一切正常,但是当我没有放置任何断点时,应用程序在数据保存之前关闭.
这是我的代码:
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
await api.BeforeClosing(true);
deferral.Complete();
}
public async Task BeforeClosing(Boolean toTombstoning)
{
SaveItem<LoadingProgress>(fileNameLoadingProgress, InitLoading);
}
public static async void SaveItem<T>(String fileName, T data, Boolean crossThreadSecure = false) where T : IBinarySerializable
{
await CreateNewStorageFile(fileName);
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFolder dataFolder = await localFolder.GetFolderAsync("Data");
StorageFile file = await dataFolder.GetFileAsync(fileName);
using (var stream = await file.OpenStreamForWriteAsync())
{
using (var writer = new BinaryWriter(stream))
{
writer.Write<T>(data);
}
} …Run Code Online (Sandbox Code Playgroud)