我正在使用 blazor 的服务器端。
这是一个名为 Child 的子组件:
<div class="Child">
<img @OnClick="ShowFullImage" src="/img/aaa.jpg"/>
</div>
@code{
private void ShowFullImage(){}
}
Run Code Online (Sandbox Code Playgroud)
这是一个父组件:
<Child></Child>
<img id="FullImage"/>
Run Code Online (Sandbox Code Playgroud)
子组件即将显示缩略图。当用户单击缩略图时(仅img在子组件中),完整图像将显示在img名为 FullImage 的父组件中。
现在的问题是,虽然我可以onlick在子组件中添加一个函数,但我还不知道如何访问它的父组件。
我想创建一个内存中的 SQLite 数据库。
这是startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<TestDBContext>().AddEntityFrameworkSqlite();
}
Run Code Online (Sandbox Code Playgroud)
这是数据库的模型:
public class TestModel
{
public string UserName { get; set; }
[Key]
public string id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是数据库的DBContext:
public class TestDBContext : DbContext
{
public virtual DbSet<TestModel> Test { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=:memory:");
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制器:
private readonly TestDBContext TestDBContext;
public HomeController(ILogger<HomeController> logger,TestDBContext _TestDBContext)
{
_logger = logger;
this.TestDBContext = _TestDBContext;
}
public IActionResult Index() …Run Code Online (Sandbox Code Playgroud) entity-framework entity-framework-core .net-core asp.net-core
我想尝试通过 Blazor 客户端连接到计算机。
我安装了 Nuget 包 System.Net.Sockets 来实现它。
这是代码:
@page "/fetchdata"
@inject HttpClient Http
@using System.Net.Sockets;
@using System.Net;
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@code {
Socket S;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
IPAddress ipAddress = IPAddress.Parse("192.168.200.111");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 23);
try
{
S = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
S.Connect(ipEndPoint);
Byte[] inBuffer = new Byte[1024];
S.Send(System.Text.Encoding.Default.GetBytes("PEY" + Environment.NewLine));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当断点运行在 …
现在我只想在鼠标悬停时将按钮的背景更改为#f5f5f5.
WPF具有触发功能,可以轻松完成.
虽然我听说UWP不再有触发器,但是使用其他功能,就像这个问题:
UWP项目不支持触发元素(XAML)
我使用DataTriggerBehavior作为教程:
<ControlTemplate TargetType="Button" x:Key="ButtonControlTemplate">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Name="ButtonBorder" Background="{TemplateBinding Background}">
<ContentPresenter FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Foreground="{TemplateBinding Foreground}"></ContentPresenter>
</Border>
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding PointerMovedEvent, ElementName=ButtonBorder}" Value="true">
<Core:ChangePropertyAction TargetObject="{Binding ElementName=ButtonBorder}" PropertyName="Background" Value="#f5f5f5" />
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
该程序可以成功运行,但不能改变背景.
哦,我的上帝.
我也尝试过VisualState,但我找不到教程,所以我不知道如何使用它.
更重要的是,我几乎不知道为什么微软不再使用触发器了.
你能帮我解决一下我的问题吗?
非常感谢!
现在我需要连接到第三方API。
API 需要设置Content-Type为application/json;charset=UTF-8.
我是这样实现的:
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.aaa.com");
request.Content = new StringContent(IP);
request.Headers.Add("Content-Type", "application/json;charset=UTF-8");
var client = clientFactory.CreateClient();
client.Timeout = TimeSpan.FromSeconds(5);
var response = await client.SendAsync(request);
string Content = "";
if (response.IsSuccessStatusCode)
{
Content = await response.Content.ReadAsStringAsync();
return Content;
}
else
{
return "";
}
Run Code Online (Sandbox Code Playgroud)
但是,它会引发错误:
{"Misused header name, 'Content-Type'. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects."}
Run Code Online (Sandbox Code Playgroud)
很快我通过修改代码找到了解决方案,如下所示:
var request = new HttpRequestMessage(HttpMethod.Post, …Run Code Online (Sandbox Code Playgroud) 这是一堂课:
public class ABC
{
public string A;
public string B;
}
Run Code Online (Sandbox Code Playgroud)
我想从A等于123的列表中删除该类.这里是代码:
List<ABC> L = new List<ABC>();
ABC ClassToRemove=null;
foreach (ABC Single in L)
{
if (Single.A == "123")
{
ClassToRemove = Single;
break;
}
}
if (ClassToRemove != null)
{
L.Remove(ClassToRemove);
}
Run Code Online (Sandbox Code Playgroud)
这段代码可以做到这一点.但是我觉得它太长而且难看.我想知道是否有更快的方法来实现它并且代码更好一些?
我的asp.net core程序需要收集前一天访问网站的所有IP。
当一天结束时,我需要将 IP 列表输出到 txt 文件。
因此,程序应该每天运行一次保存 IP 列表的方法。
我应该如何设计它?
在我看来,我会在 中添加一个循环任务startup.cs,并将 设为Task.Delay24 小时(一天)。
我应该这样做吗?谢谢。
我的操作系统是Windows 10(内部预览).我安装了Microsoft Visual Studio Enterprise 2017 15.2(26430.15),这是最新版本.我想使用通用Windows平台(UWP)模板,但我找不到它.
现在我不能写UWP程序了.
我的Visual Studio出了什么问题?我错过了什么吗?谢谢.
我正在制作一个自定义的 SaveFileDialog。
这是一个目录:
C:\Windows\System32\0409
Run Code Online (Sandbox Code Playgroud)
它可读但不可写。
我通常使用这种方式来了解它是否可读:
foreach (string i in Directory.GetDirectories(@"C:\Windows\System32\", "*", new EnumerationOptions { IgnoreInaccessible = true }))
{
////
}
Run Code Online (Sandbox Code Playgroud)
但是,这种方式无法获取是否可写。
当程序将文件写入不可写目录时,会抛出以下错误:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access to the path 'C:\Windows\System32\0409\' is denied.
Source=System.IO.FileSystem
StackTrace:
at System.IO.FileSystem.CreateDirectory(String fullPath, Byte[] securityDescriptor)
at System.IO.Directory.CreateDirectory(String path)
at CoolDuck.Dialogs.Extract.<Window_Loaded>b__26_0() in G:\SampleProject\Test.xaml.cs:line 128
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__277_0(Object obj)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
Run Code Online (Sandbox Code Playgroud)
我不想用 atry&catch来解决这个问题。我不认为这是正确的方法。
我该如何解决这个问题?谢谢你。