我正在尝试学习如何使用IValueConverter.我有以下转换器:
[ValueConversion(typeof(string), typeof(string))]
public class RequiredFieldConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return "";
return value.ToString() + "*";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return "";
var str = value.ToString();
return str+"Convert Back testing";
}
}
Run Code Online (Sandbox Code Playgroud)
我在app.xaml文件中添加了RequiredFieldConverter资源,我想尝试将其作为:
<TextBox Name="textBox2" Width="120" />
<TextBox Text="{Binding ElementName=textBox2, Path=Text, Converter=RequiredFieldConverter}" Name="textBox3" Width="120" />
Run Code Online (Sandbox Code Playgroud)
我希望当我在textbox2中键入"hello"时,它会在textbox3中显示"hello*",但它不起作用.实际上我在运行时遇到以下异常:
{"无法将'System.String'类型的对象强制转换为'System.Windows.Data.IValueConverter'."}
我也知道值转换器功能正在工作,因为它可以在我这样做时工作:
Content="{Binding Source={StaticResource Cliente}, Converter={StaticResource RequiredFieldConverter}}"
Run Code Online (Sandbox Code Playgroud) 我有枚举:
[Flags]
enum Editions
{
Educational,
Basic,
Pro,
Ultra
}
Run Code Online (Sandbox Code Playgroud)
为什么我会这样做?
var x = Editions.Basic;
var y = Editions.Educational;
var test =x.HasFlag(y); // why is this true!?
// and!!!
var test2 = y.HasFlag(x); // this is false!
Run Code Online (Sandbox Code Playgroud) 如果我有
<StackPanel>
<Button name="btn"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
btn
当我按下那个按钮时,如何将Button替换为不同的控件?在这种情况下,我知道父对象是StackPanel,所以我可以将父对象转换为stackPanel,然后替换该子对象.但是如果我有边框怎么办?
<Border>
<Button name="btn"/>
</Border>
Run Code Online (Sandbox Code Playgroud)
感谢McGarnagle,我创建了这个扩展方法
如何在不事先知道按钮的父级的情况下用自定义控件替换Button?
public static void ReplaceWith(this FrameworkElement elementToReplace, FrameworkElement newControl)
{
newControl.Width = elementToReplace.Width;
newControl.Height = elementToReplace.Height;
newControl.Margin = elementToReplace.Margin;
// get parent of control
var parent = elementToReplace.Parent;
if (parent is Panel)
{
var panel = (Panel)parent;
for (var i = 0; i < panel.Children.Count; i++)
{
if (panel.Children[i] == elementToReplace)
{
panel.Children.RemoveAt(i);
panel.Children.Insert(i, newControl);
break;
}
}
}
else if (parent is Decorator)
{
((Decorator)parent).Child = …
Run Code Online (Sandbox Code Playgroud) 在我的客户端应用程序中,我有时连接到localhost:1242\SomeService.asmx
它,有时连接到 它someDomain:1242\SomeService.asmx
.换句话说,有时我想在本地测试,有些时候需要远程测试.
VS为您提供的默认选项是调试和发布.我想创建自定义的,实际上我刚刚创建了一个新的构建配置:
无论如何,如果我使用该配置,我怎么能在代码中知道?
我想做的事情如下:
if(Configuration.Type == ConfigTypes.Local)
ConectionString = "localhost:1242:\SomeService.asmx";
else if (Configuration.Type == ConfigTypes.Remote1)
ConectionString = "SomeDomain1:1242:\SomeService.asmx";
else if (Configuration.Type == ConfigTypes.Remote2)
ConectionString = "SomeDifDomain:1242:\SomeService.asmx";
Run Code Online (Sandbox Code Playgroud)
释放模式往往更有效率?我该如何指定这些设置?
我正在使用从这个链接获得的代码:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#define DATA "Hello world"
int main()
{
int sock;
struct sockaddr_un server;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
perror("opening stream socket");
exit(1);
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, "/tmp/foo.sock");
int con = connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un));
if (con < 0) {
close(sock);
perror("connecting stream socket");
exit(1);
}
const char * D = "Hello world!";
if (write(sock, DATA, sizeof(DATA)) < 0)
perror("writing on stream …
Run Code Online (Sandbox Code Playgroud) 我在.cs文件中的项目中有一个名为CreateListView的类,我可以通过在页面顶部的uses中包含它的命名空间来使用它.有没有办法我可以编译文件,以便我仍然可以使用该类,但用户无法看到该类的内容.我希望用户仍然能够从该类创建对象,但我不希望他们修改它,如果他们看不到它也会更好.
有时我会在不释放物体时感到困惑.我有:
NSTimer *timer2;
timer2 = [NSTimer scheduledTimerWithTimeInterval: (5)
target: self
selector: @selector(someMethod:)
userInfo: nil
repeats: YES];
Run Code Online (Sandbox Code Playgroud)
并且每五秒执行一次的方法是:
-(void) someMethod:(NSTimer*)theTimer
{
NSLog(@"code got executed");
}
Run Code Online (Sandbox Code Playgroud)
我有另一种方法,在我的根视图控制器上放置另一个nib文件:
ViewControllerObjetivos *control = [ViewControllerObjetivos alloc];
[control initWithNibName:@"ViewControllerObjetivos" bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc]
initWithRootViewController:control];
[self presentModalViewController:navControl animated:NO];
[navControl setNavigationBarHidden:YES];
[control release];
[navControl release];
Run Code Online (Sandbox Code Playgroud)
当我调用最后一个方法时,新的nib文件放在根视图控制器上.而someMethod仍被调用!
所以我很困惑,如果我应该释放timer2因为我没有使用单词init或alloc也没有复制来初始化它.那么我想做什么呢?我应该停止它然后调用我显示的最后一个方法吗?或者我应该发布整个nib文件,因为我将要使用新的nib文件?
我经常需要为wpf中不属于视图的一些内容设置动画,例如计算机的音量或鼠标的位置.我想通过使用wpf故事板和内置的缓动功能来实现.
例如,假设我想使用以下故事板在我的计算机上设置动画(减小音量):
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="someProperty"
Storyboard.TargetName="SomeTarget">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
Run Code Online (Sandbox Code Playgroud)
在我的代码后面我用函数设置音量:
MyVolumeController.SetVolume(0);
Run Code Online (Sandbox Code Playgroud)
因此,我想创建一个看起来像这样的函数:(注意函数是某种伪代码)
public void AnimateProperty(Storyboard sb, Action<double> onPropertyChange)
{
var property = //sb.targetProperty;
property.OnValueChanged += (a,b)=>{
onPropertyChange(b.newValue);
}
sb.begin();// start animating
}
Run Code Online (Sandbox Code Playgroud)
然后我可以通过调用该方法为该动画设置动画:
AnimateProperty(
(Storyboard)this.FindResource("Storyboard1"), // storyboard
(newVal)=>{MyVolumeController.SetVolume(newVal) // action
);
Run Code Online (Sandbox Code Playgroud) 我需要解析类似于XML的大文本.因为它不在内存中的文本(我有一个StreamReader对象)将该流放在内存中是我花费最多时间的地方.所以在一个线程上我将该流放入一个数组(内存).我有另一个处理该数组的线程.但我有很奇怪的行为.例如,看一下这个图像:
请注意,listToProcess[counter] = buffer
现在应该listToProcess[10] = buffer
注意调试器说明listToProcess[10]=null
为什么!?.另一个线程只读取它不修改它们的项目.起初我以为可能另一个线程正在使该item = null但事实并非如此.为什么我会遇到这种行为?
如果你想在这里看到我的代码,它是:
Semaphore sem = new Semaphore(0, 1000000);
bool w;
bool done = false;
// this task is responsible for parsing text created by main thread. Main thread
// reads text from the stream and places chunks in listToProces[]
var task1 = Task.Factory.StartNew(() =>
{
sem.WaitOne(); // wait so there are items on list (listToProcess) to work with
// counter to identify which chunk of char[] in …
Run Code Online (Sandbox Code Playgroud) 我想说:
MyCollection = new ConcurrentDictionary<string, int>();
Run Code Online (Sandbox Code Playgroud)
我现在可以安全地添加和删除项目MyCollection
.但是如何修改项目呢.例如,它是否安全:
MyCollection["key"] = 1; // thread 1
Run Code Online (Sandbox Code Playgroud)
和
MyCollection["key"] = 2; // thread 2
Run Code Online (Sandbox Code Playgroud)
MyCollection2 = new ConcurrentDictionary<string, List<int>>();
Run Code Online (Sandbox Code Playgroud)
这样做安全吗?
MyCollection2["key"].Add(1); // Thread1
Run Code Online (Sandbox Code Playgroud)
和
MyCollection2["key"].Add(2); // Thread2
Run Code Online (Sandbox Code Playgroud)
其中Thread1和Thread2同时执行.修改项目时是否必须创建锁定?
我有一个文件MyUtilities.exe
.
当我跑:
Process.Start("MyUtilities.exe","SomeParameter");
如果在没有管理员权限的情况下运行,则该进程的退出代码为0(OK).如果我以管理员身份运行该代码,退出代码为1!
由于我没有创建MyUtilities.exe
我无法修改它.
最后我需要以Process.Start("MyUtilities.exe","SomeParameter");
管理员身份运行并让它返回退出代码= 0.我设法做到这一点的方法是将其兼容性更改为:
(右键单击文件 - >属性 - >兼容性 - >以管理员身份运行此程序)
在更改之后,我现在能够以Process.Start("MyUtilities.exe","SomeParameter");
管理员身份运行并返回退出代码0.
所以我的问题是如何更改该文件与代码的兼容性,这样我就不必告诉用户右键单击该文件然后更改设置.
让我们说我有班级:
class Foo
{
public Foo(int value)
{
this.Value = value;
}
public int Value { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
我想做以下事情:
var f = new Foo(1);
f = 5; // How can I do this? In here I want to change => f.Value
var x = f.Value; // I will like x to equal to 5 in here
Run Code Online (Sandbox Code Playgroud) c# ×10
wpf ×3
binding ×1
c ×1
cocoa-touch ×1
enums ×1
flags ×1
ios ×1
iphone ×1
objective-c ×1
performance ×1
privileges ×1
process ×1
release ×1
semaphore ×1
storyboard ×1
unix-socket ×1