在C#中,我找到了一个非常好的方法,它允许你从指定的控件中获取所有后代和所有的后代.
我正在为JavaFX寻找类似的方法.
我看到Parent该类是我想要使用的类,因为它是从中派生所有带子项的Node类的类.
这就是我到目前为止(我还没有真正在谷歌上找到任何搜索,比如"JavaFX从场景中获取所有节点"):
public static ArrayList<Node> GetAllNodes(Parent root){
ArrayList<Node> Descendents = new ArrayList<>();
root.getChildrenUnmodifiable().stream().forEach(N -> {
if (!Descendents.contains(N)) Descendents.add(N);
if (N.getClass() == Parent.class) Descendents.addAll(
GetAllNodes((Parent)N)
);
});
}
Run Code Online (Sandbox Code Playgroud)
那么如何判断N是父母(或从父母延伸)?我这样做了吗?它似乎没有工作......它从根(父)节点抓取所有节点,但不从其中包含子节点的节点抓取.我觉得这可能是一个答案,但我只是问这个问题......错了.我该怎么做呢?
这里问的一个问题的延续:
在上述问题中,我有以下函数返回Task类型的对象(用于增量测试目的):
private static Task<object> GetInstance( ) {
return new Task<object>( (Func<Task<object>>)(async ( ) => {
await SimpleMessage.ShowAsync( "TEST" );
return new object( );
} ) );
}
Run Code Online (Sandbox Code Playgroud)
当我调用时await GetInstance( );,函数被调用(我假设任务被返回,因为没有抛出异常),但随后任务就在那里.
我只能猜测我做错了.
我不希望此函数返回已在运行的任务(即IMPERATIVE).
如何异步运行此函数返回的任务?
我有一个静态类,它从应用程序集中读取信息.
我已经声明它是静态的,因为类不需要实例声明,只能直接从应用程序范围读取.我有一个带有几个标签的控件,我想用它来显示一些这些信息.
我怎样才能将控件DataContext设置为等于类?
码:
/// <summary>
/// Class for Reading Program Information.
/// </summary>
public static class ProgramInfo {
private static Assembly ProgramAssembly = Assembly.GetEntryAssembly( );
/// <summary>
/// Get Program Name
/// </summary>
public static string ProgramName {
get { return ProgramInfo.ProgramAssembly.GetCustomAttribute<AssemblyProductAttribute>( ).Product; }
}
/// <summary>
/// Get Program Build Date
/// </summary>
public static DateTime BuildDate {
get { return File.GetLastWriteTime( ProgramInfo.ProgramAssembly.Location ); }
}
/// <summary>
/// Get Program Version (Major.Minor)
/// </summary>
public static string …Run Code Online (Sandbox Code Playgroud) 我有以下一点代码 -
await Task.WhenAll(TaskList /*List of Task objects*/);
await AnotherAwaitableMethod( );
Run Code Online (Sandbox Code Playgroud)
这很好,并且是必要的,因为它AnotherAwaitableMethod依赖于确保TaskList在执行之前完成任务.
但是,我希望能够说出类似的话
await Task.WhenAll(TaskList).ContinueWith( /*AnotherAwaitableMethod call?*/ );
Run Code Online (Sandbox Code Playgroud)
这可能吗?我误解了目的Task.ContinueWith吗?
我有两节课:
public class Question : IDisposable, IEquatable<Question>
{
}
public class SessionQuestion : Question, IDisposable, IEquatable<SessionQuestion>
{
}
Run Code Online (Sandbox Code Playgroud)
作为Question继承IDisposable和IEquatable,是否也SessionQuestion隐式继承了这些接口?
给出以下扩展以防止Tasks阻塞UI线程(可能不是完全正确的术语,但无论如何):
public static ConfiguredTaskAwaitable DontBlock( this Task T ) {
return T.ConfigureAwait( false );
}
public static ConfiguredTaskAwaitable<T> DontBlock<T>( this Task<T> T2 ) {
return T2.ConfigureAwait( false );
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下(例如,如果我需要在对象构造函数中调用awaitable,或者如果我需要.Wait( )从WinForms Program.Main( )方法调用),我需要执行以下操作:
public class Foo{
public Foo( ){
//I know full well that the result from DontBlock does not have a 'wait' method, so of course this will fail miserably.
AwaitableBar.DontBlock( ).Wait( );
}
}
Run Code Online (Sandbox Code Playgroud)
async如果我不能打电话.Wait( ),如何在函数/方法之外"等待" 它?我看到它有一个.GetAwaiter( )方法可以返回一个 …
我有一个IValueConverter我想用它来做简单的数学运算,它具有以下Convert功能:
public object Convert(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
if (parameter == null)
{
return value;
}
switch (((string)parameter).ToCharArray()[0])
{
case '%':
return (double)value % double.Parse(
((string)parameter).TrimStart(new char[] {'%'}));
case '*':
return (double)value * double.Parse(
((string)parameter).TrimStart(new char[] {'*'}));
case '/':
return (double)value / double.Parse(
((string)parameter).TrimStart(new char[] {'/'}));
case '+':
return (double)value + double.Parse(
((string)parameter).TrimStart(new char[] {'+'}));
case '-':
if (((string)parameter).Length > 1)
{
return (double)value - double.Parse(
((string)parameter).TrimStart(new char[] {'-'}));
}
else …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过Inno Setup(以及其他应用程序)安装最新的平台(x64或x86)相应的Java Runtime Environment.我找到了一些脚本示例,说明如何检测版本并安装如果正确并根据我的需要调整它们但我仍然遇到这样:
无法打开文件"path\to\JREInstall.exe":
CreateProcess失败:代码5:
访问被拒绝
这是严格负责安装JRE的代码:
[Setup]
AppName="JRE Setup"
AppVersion=0.1
DefaultDirName="JRE Setup"
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Files]
Source: "jre-8u11-windows-x64.exe"; DestDir: "{tmp}\JREInstall.exe"; \
Check: IsWin64 AND InstallJava();
Source: "jre-8u11-windows-i586.exe"; DestDir: "{tmp}\JREInstall.exe"; \
Check: (NOT IsWin64) AND InstallJava();
[Run]
Filename: "{tmp}\JREInstall.exe"; Parameters: "/s"; \
Flags: nowait postinstall runhidden runascurrentuser; Check: InstallJava()
[Code]
procedure DecodeVersion(verstr: String; var verint: array of Integer);
var
i,p: Integer; s: string;
begin
{ initialize array }
verint := [0,0,0,0];
i := 0;
while ((Length(verstr) > …Run Code Online (Sandbox Code Playgroud) 我在异步等待方面相当落后,所以这可能是一个“duh”问题。
我正在开发一个非常小的 UI 应用程序,该应用程序使用WPF NotifyIcon库从系统托盘运行。
应用程序应该以以下方式非常简单地运行(对于用户而言):
我遇到的问题是“异步执行开始”部分。在此之前发生的所有事情都运行良好,但是当程序开始尝试“运行”时,UI 会锁定(我的意思是,用户可以像疯子一样单击托盘图标,并且上下文菜单拒绝出现)。
这种锁定发生的时间长得令人无法接受。
这是启动代码:
private async void AppStartup( object sender, StartupEventArgs e ) {
this.TRSIcon = this.FindResource( "TRSIcon" ) as TaskbarIcon;
if ( Settings.Default.DoUpgrade ) { //Upgrade if necessary.
Settings.Default.Upgrade( );
Settings.Default.DoUpgrade = false;
Settings.Default.Save( );
}
if ( string.IsNullOrEmpty( Settings.Default.Username ) || string.IsNullOrEmpty( Settings.Default.Password ) ) {
new Help( ).ShowDialog( );
Tuple<string, string> UP;
if ( ( UP = Login.Instance.GetUserPassword( ) ) != null ) …Run Code Online (Sandbox Code Playgroud) 我希望能够实现一个字典,其中键的Type值和Func<T>where 值T是与键相同类型的对象:
Dictionary<Type, Func<T>> TypeDictionary = new Dictionary<Type, Func<T>>( ) /*Func<T> returns an object of the same type as the Key*/
TypeDictionary.Add( typeof( int ), ( ) => 5 );
TypeDictionary.Add( typeof( string ), ( ) => "Foo" );
Run Code Online (Sandbox Code Playgroud)
因此,基本上,字典将填充引用Func<T>将返回该值的类型:
int Bar = TypeDictionary[ typeof( int ) ]( );
string Baz = TypeDictionary[ typeof( string ) ]( );
Run Code Online (Sandbox Code Playgroud)
我该如何实施和执行此操作?
c# ×8
async-await ×4
asynchronous ×3
wpf ×3
java ×2
task ×2
xaml ×2
datacontext ×1
dictionary ×1
generics ×1
inheritance ×1
inno-setup ×1
javafx ×1
javafx-8 ×1
permissions ×1
recursion ×1
static ×1
types ×1