异步任务到 Youtube
Async Task to Youtube
实际上我是新手。我有一个简短的应用程序,只是为了检查应用程序是否可以从 youtube 异步获取身份验证,然后 return 应用程序回到它的轨道。这是我的代码片段
private async void button1_Click(object sender, RoutedEventArgs e)
{
await YoutubeAuth();
MessageBox.Show(token);
}
private async Task YoutubeAuth()
{
OAUth2Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets { ClientId = YoutubeClientId, ClientSecret = YoutubeClientSecret },
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
token = OAUth2Credential.Token.TokenType;
}
代码MessageBox.Show(token);
从未执行过。
编辑:
我已经尝试了其他更简单的代码,如下所示,但 MessageBox 仍然没有被触发
private async void button1_Click(object sender, RoutedEventArgs e)
{
await YoutubeAuth();
MessageBox.Show(token);
}
private async Task YoutubeAuth()
{
token = "test token";
}
我的 猜测 是您创建了一个名为 button1
的按钮,然后您编写了一个名为 button1_Click
的方法,但您从未将两者联系在一起.
在常见的 .Net UI 框架(Winforms、WPF)中,这是行不通的,因为方法的名称实际上并不重要。重要的是按钮设置为在单击时调用方法。具体如何执行此操作取决于 UI,但我相信双击设计器中的按钮应该会创建将在您单击时调用的方法。
这看起来很有趣。我已经快速编写了 WPF 示例应用程序来验证
MainWindow.xaml
<Window x:Class="TestAsyncTaskToYoutube.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestAsyncTaskToYoutube"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Content="Button" />
</Grid>
</Window>
MainWindow.xaml.cs
using System.Threading.Tasks;
using System.Windows;
namespace TestAsyncTaskToYoutube
{
public partial class MainWindow : Window
{
private string token;
public MainWindow()
{
InitializeComponent();
button.Click += button_Click;
}
private async void button_Click(object sender, RoutedEventArgs e)
{
await YoutubeAuth();
MessageBox.Show(token);
}
private Task<int> YoutubeAuth()
{
token = "test token";
return Task.FromResult(0);
}
}
}
这里没问题。 MessageButton 按原样触发。我确定您的代码在任何地方都与我的有所不同:|
我们如何帮助您?
编辑:避免 Task.FromResult()(.NET 4.5 功能)
private async Task YoutubeAuth()
{
token = "test token";
}
实际上我是新手。我有一个简短的应用程序,只是为了检查应用程序是否可以从 youtube 异步获取身份验证,然后 return 应用程序回到它的轨道。这是我的代码片段
private async void button1_Click(object sender, RoutedEventArgs e)
{
await YoutubeAuth();
MessageBox.Show(token);
}
private async Task YoutubeAuth()
{
OAUth2Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets { ClientId = YoutubeClientId, ClientSecret = YoutubeClientSecret },
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
token = OAUth2Credential.Token.TokenType;
}
代码MessageBox.Show(token);
从未执行过。
编辑:
我已经尝试了其他更简单的代码,如下所示,但 MessageBox 仍然没有被触发
private async void button1_Click(object sender, RoutedEventArgs e)
{
await YoutubeAuth();
MessageBox.Show(token);
}
private async Task YoutubeAuth()
{
token = "test token";
}
我的 猜测 是您创建了一个名为 button1
的按钮,然后您编写了一个名为 button1_Click
的方法,但您从未将两者联系在一起.
在常见的 .Net UI 框架(Winforms、WPF)中,这是行不通的,因为方法的名称实际上并不重要。重要的是按钮设置为在单击时调用方法。具体如何执行此操作取决于 UI,但我相信双击设计器中的按钮应该会创建将在您单击时调用的方法。
这看起来很有趣。我已经快速编写了 WPF 示例应用程序来验证
MainWindow.xaml
<Window x:Class="TestAsyncTaskToYoutube.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestAsyncTaskToYoutube"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Content="Button" />
</Grid>
</Window>
MainWindow.xaml.cs
using System.Threading.Tasks;
using System.Windows;
namespace TestAsyncTaskToYoutube
{
public partial class MainWindow : Window
{
private string token;
public MainWindow()
{
InitializeComponent();
button.Click += button_Click;
}
private async void button_Click(object sender, RoutedEventArgs e)
{
await YoutubeAuth();
MessageBox.Show(token);
}
private Task<int> YoutubeAuth()
{
token = "test token";
return Task.FromResult(0);
}
}
}
这里没问题。 MessageButton 按原样触发。我确定您的代码在任何地方都与我的有所不同:|
我们如何帮助您?
编辑:避免 Task.FromResult()(.NET 4.5 功能)
private async Task YoutubeAuth()
{
token = "test token";
}